PrimeFaces - DataTable Sorting Example

[Updated: Jun 10, 2017, Created: Jun 9, 2017]

Creating managed bean

@ManagedBean
@ViewScoped
public class EmployeeBean {
  private List<Employee> employeeList = new ArrayList<>();
    .............
  public List<Employee> getEmployeeList () {
      return employeeList;
  }
}
public class Employee {
  private long id;
  private String name;
  private String phoneNumber;
  private String address;
  //getters/setters
    .............
}

The view

src/main/webapp/index.xhtml

<p:dataTable var="employee" value="#{employeeBean.employeeList}">
    <p:column headerText="Id" sortBy="#{employee.id}">
        <h:outputText value="#{employee.id}"/>
    </p:column>

    <p:column headerText="Name" sortBy="#{employee.name}">
        <h:outputText value="#{employee.name}"/>
    </p:column>

    <p:column headerText="Phone Number" sortBy="#{employee.phoneNumber}">
        <h:outputText value="#{employee.phoneNumber}"/>
    </p:column>

    <p:column headerText="Address" sortBy="#{employee.address}">
        <h:outputText value="#{employee.address}"/>
    </p:column>
</p:dataTable>

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

Output

Example Project

Dependencies and Technologies Used :

  • primefaces 6.1 primefaces
  • jsf-api 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • datafactory 0.8: Library to generate data for testing.
  • JDK 1.8
  • Maven 3.3.9

Primefaces DataTable Sorting Example Select All Download
  • data-table-sorting
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp

See Also