Sortable & Filterable PrimeFaces DataTable

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

Or an alternative to the above:

<form>
    <p:dataTable 
        value="#{customerSessionBean.customers}" 
        var="item">
        <p:column 
            filterBy="${item.name}" sortBy="${item.name}" 
            headerText="Name">${item.name}
        </p:column>
        <p:column 
            filterBy="${item.city}" sortBy="${item.city}" 
            headerText="City">${item.city}
        </p:column>
        <p:column 
            filterBy="${item.state}" sortBy="${item.state}" 
            headerText="State">${item.state}
        </p:column>
    </p:dataTable>
</form>

That gives me this:

And here's the filter in action:

Behind this, I have:

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

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

    @EJB
    private CustomerSessionBean customerSessionBean;

    public ResultManagedBean() {
    }

    private List<Customer> customers;

    @PostConstruct
    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.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class CustomerSessionBean implements Serializable{

    @PersistenceContext
    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.

Comments:

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:
http://www.primefaces.org/showcase-labs/ui/datatableFiltering.jsf

For the individual looking for lazy loading, this is also very easily done in PF. Check out this example:
http://www.primefaces.org/showcase-labs/ui/datatableLazy.jsf

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
About

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.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today