Sortable & Filterable PrimeFaces DataTable

    <p:dataTable value="#{resultManagedBean.customers}" var="customer">
        <p:column id="nameHeader" filterBy="#{}" sortBy="#{}">
            <f:facet name="header">
                <h:outputText value="Name" />
            <h:outputText value="#{}" />
        <p:column id="cityHeader" filterBy="#{}" sortBy="#{}">
            <f:facet name="header">
                <h:outputText value="City" />
            <h:outputText value="#{}" />

Or an alternative to the above:

            filterBy="${}" sortBy="${}" 
            filterBy="${}" sortBy="${}" 
            filterBy="${item.state}" sortBy="${item.state}" 

That gives me this:

And here's the filter in action:

Behind this, I have:

import com.mycompany.mavenproject3.entities.Customer;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;

@Named(value = "resultManagedBean")
public class ResultManagedBean implements Serializable {

    private CustomerSessionBean customerSessionBean;

    public ResultManagedBean() {

    private List<Customer> customers;

    public void init(){
        customers = customerSessionBean.getCustomers();
    public List<Customer> getCustomers() {
        return customers;

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;


And the above refers to the EJB below, which is a standard EJB that I create in all my Java EE 6 demos:

import com.mycompany.mavenproject3.entities.Customer;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class CustomerSessionBean implements Serializable{

    EntityManager em;
    public List getCustomers() {
        return em.createNamedQuery("Customer.findAll").getResultList();


Only problem is that the columns are only sortable after the first time I use the filter.


it looks like you are using primefaces and that's not even in the xhtml, could you at least show how this is done if it's a lazy loading data?

Posted by guest on October 30, 2012 at 06:52 PM PDT #

Don't understand your question. PrimeFaces is used because the p: namespace at the top of the file defines it.

Posted by Geertjan on October 30, 2012 at 08:25 PM PDT #

What version of Primefaces are you using? The reason I ask is that they have recently changed the way you need to perform filtering and how you have done it is no longer being supported moving forward. You need to provide another List<Customer> filteredCustomers .... and provide this to the table's filteredValue parameter. See the following example:

For the individual looking for lazy loading, this is also very easily done in PF. Check out this example:

Posted by guest on October 31, 2012 at 05:43 AM PDT #

Thank you for helping me Mr.Geertjan Wielenga

Posted by Mohamed Hejaiej on December 02, 2013 at 01:38 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« August 2015