A recent requirement on one of our ADF 11g projects: we want to gather a number of settings a user has applied to the rich table in one of the pages. These settings include position, width and visibility of columns (supported by change persistence) and custom data filter, sort configuration and highlight condition. These latter settings are not part of change persistence out of the box. An additional requirement is that this collection of settings is to be saved under a label and to be made available to all users for selection when they view that same table.
I like these requirements. They are tangible, concrete and add fun(ctionality) to the application that directly pleases the end users. With ADF 11g, it is not an extremely difficult requirement – it seems.
One of the aspects mentioned here – sort setting – requires us not only to intercept or capture changes in sort settings – which sortListener on the columns of the table will do nicely – but also the programmatic application of sort settings when the table is rendered and a ‘user saved packet of table settings’ is selected.
In this article I take a very brief look at how to programmatical set the sorting condition on the rich table’s columns.
Visually this functionality can be illustrated like this:
The table is shown with data that is currently sorted by Job. When the user clicks the button Sort by Name, our programmatic ‘set the sort property on the table’ code is invoked and the data is re-rendered, sorted by Name:
The underlying code is quite simple.
I have created a class TableManager and based on that class a managed bean called tableManager. This class has a property RichTable table with setter and getter method.
The <af:table> is bound to the managed bean through an EL expression:
The TableManager class contains a method sortByName:
This method creates a new sort criterion, based on the name property (which is one of the properties in the Employee class that is used for the objects in hrmBean.employees). This criterion is added to an otherwise empty list that is set as SortCriteria on the RichTable. Next, the table is added as partial target to the ADFFacesContext, which means that when the request completes, the table component is refreshed in the browser.
Finally, the button has its action property (could have been the actionListener as well) set to refer to the sortByName method:
When the button is pressed, the tableManager bean is invoked, method sortByName is executed, the sortCriteria on the table are manipulated and the table gets refreshed. Job done.