Tuesday Oct 30, 2012

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.

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
« October 2012 »
SunMonTueWedThuFriSat
 
1
2
3
21
   
       
Today