Rich Tables are powerful beasts in ADF Faces. They can present tons of information in many different ways. They allow users an extensive range of manipulation – from reordering and hiding columns, changing column width to sorting and filtering the data presented in the table and detaching the table from its embedding page as well as exporting the data from the table to an Excel file. Pretty impressive. And many of the visual configurations can be stored using Change Persistence across requests and even across sessions.
One specific requirement on a recent ADF 11g project led me to investigating if and how the ordering of columns in a rich table can be manipulated programmatically (rather than having the user to it). Earlier articles on this blog discussed similar challenges: intercepting changes in the column order applied by a user (https://technology.amis.nl/2012/02/intercepting-user-customization-such-as-column-reordering-in-tables-in-adf-11g-change-persistence/) and programmatically setting the sort order for columns in a Rich Table (https://technology.amis.nl/2012/03/adf-11g-programmatically-configuring-sort-for-rich-table/). As in these cases, the solution turns out to be fairly simple.
Changes applied at run time to the UI components in an ADF Faces page based on manipulations recorded through Change Persistence are applied through the ChangeManager class. In the same way ADF uses the ChangeManager to apply these changes read from MDS, we can also leverage the ChangeManager to apply our own changes. The article on intercepting column repositioning provided insight in the nature of the change: the repositioning of a column is recorded and applied as a change of type AttributeComponentChange. This class has a constructor that takes two arguments: the name of the attribute – displayIndex in this case – and the value of the attribute – an int specifying the new position of the column. The change can be applied to the RichColumn component using the addComponentChange method on the ChangeManager. This method takes three arguments: the current facesContext, the RichColumn on which to apply the change and the AttributeComponentChange object.
With this information, we can create the following code that complete reverses the initial ordering of columns in a table:
The method reorganizeColumnsInTable is invoked from a button in the page:
and results in the table being refreshed like this: