O'Reilly logo

PrimeFaces Cookbook by Mert Çalışkan, Oleg Varaksin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

dataTable provides sorting options based on AJAX, by enabling the sortBy attribute at 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="#{dataTableController.cars}">
<p:columnheaderText="Year" sortBy="#{car.year}">
<h:outputText value="#{car.year}" />
</p:column>
<p:columnheaderText="Name" sortBy="#{car.name}">

<h:outputText value="#{car.name}" />
</p:column>
</p:dataTable>

The appearance of a table with sorting enabled will be like the following screenshot:

There's more... ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required