Sunday, September 23, 2012

Note on Tomahawk commandSortHeader usage.

I had a hard time on creating a sortable table using JSF Tomahawk in last week. After a long searching, try and error, it is not that hard actually.

From the existing t:dataTable code, wrap the column header with t:commandSortHeader and add two more properties, sortColumn and sortAscending, into t:dataTable, just like the code shown below.

Add two member variable into the backing bean and implement Comparable interface just like this:

Here are some notes on the usage:
  1. sortColumn property in t:dataTable indicate which column name that triggered the sort. The column name is reference from the columnName property of t:commandSortHeader.
  2. sortAscending property in t:dataTable indicate the particular column sort in ascending/descending order.
  3. The JAVA class, theBean, must implement Comparable interface in order to provide the sorting facility.
  4. In the sample code above, I only have one column to sort, thus the compare function only implement one sort. If there is more than one sort, simply expand the if-else statement.

No comments: