Sorting and filtering data in dataTable

The dataTable component provides AJAX-based built-in sorting and filtering based on its columns.

How to do it…

The dataTable component provides sorting options based on AJAX by enabling the sortBy attribute at the column level. The following is the definition of a table that lists the Car data; sorting is enabled on the name and year attributes:

<p:dataTable id="sorting" var="car"
  value="#{dataTableBean.cars}">
  <p:column headerText="Year" sortBy="#{car.year}">
    <h:outputText value="#{car.year}" />
  </p:column>
  <p:column headerText="Name" sortBy="#{car.name}">
    <h:outputText value="#{car.name}" />
  </p:column>
</p:dataTable>

When sorting is enabled, the headers of those columns will have the sort direction represented ...

Get PrimeFaces Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.