Saturday, January 31, 2009

Sorting a rich:subTable

My next problem to solve is how to sort a rich:subTable. I need this because I am displaying detail rows using rich:subTable and when user clicks on the header for the main table the data should be sorted by the column clicked in subTables also.

Then what is the problem, richfaces documentation shows we can use sortPriority and sortBy properties on subTable, unfortunately after 3 days of relentless attempts I found out from the richfaces source code that subtable is not a sortable component , so you cannot use the goodies like automatic sorting complete with nice ascending and descending marking icons.

I must implement subTable sorting because without it I cant go ahead with richfaces for my project as i need master and detail sorting in almost all the screens, so I decided to come up with some solution.

One solution is to sort all the detail rows when ever user clicks on table header, like this algorithm

for each row in main table
if there are group elements for this row sort them


This works very well and it worked too,but some how i did not liked the way it is sorting all the detail rows everytime I clicked on header, as at any point of time i display only 15 rows per page. It makes sense to sort only the details of 15 rows, after some thought here is my solution

1. Building upon my previous post where I used SortingBean to help in external sorting, add a method to SortingBean which takes a list and sorts that list based on the column provided and the sorting order
2. change the subtable definition

From:





...




To:

 



...




This approach works very well because pagination will ensure that only the rendered rows are sorted as opposed to sorting all detail rows

5 comments:

Aaron said...

Hey. Is this possible without Seam? thanks.

Harsha said...

Aaron, never tried with that , the big problem would be that it is not possible to pass parameters to get the dataSource for the subtable easily. I will try out that today.

Anonymous said...

value="#{sortingBean.sort(master.detail)}"

Is this a valid binding in Seam?
I thought you can only call an Seam component's method in action or actionListener.

Harsha said...

great thing with seam is extending the EL to allow method calls with parameters. This applies to any Seam method expression, including any JSF method binding. This should have been in the standard JSF specification to make life easier !!

Unknown said...

Please post external sorting code also and sorting bean showing error in jsf page. Is there any jar file to add???