Book Image

Primefaces Cookbook Second Edition

Book Image

Primefaces Cookbook Second Edition

Overview of this book

Table of Contents (20 chapters)
PrimeFaces Cookbook Second Edition
Credits
Foreword
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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 with small arrow icons as pointed out in this image:

The dataTable component provides filtering based on AJAX by enabling the filterBy attribute for the columns. The following is...