I'm building a WebApp in jsf 2.0 and it's about storing information and displaying it on the screen. so I've put in some "http://java.sun.com/jsf/html" dataTables to disply some lists. My Java code returns a List and then displays them on screen. but now I have to Sort the columns alphabetically. I believe there's no built in way of doing this, so i'm goign to have to look elsewhere for the task.
I came across This dataTables example, it's pretty awesome but I think I can't give it a List and display the list.
I also came across BalusC's way of incorporating the sorting intot he DataTbal, which is nice, I'm looking for this, but maybe with a jQuery IU.
Is there such an API that can meet my needs? can you point me in the right direction, or do you suggest one? I'd really want one that I just hand over the list in Java, but If I must, I can modify the data to comply with JSON format or some other...
With the native
<h:dataTable>
you have to do the sorting yourself in your managed bean. You could use existing JSF extension libraries that have them built in such as:But if you don't want to use the above toolkits, then in your managed bean, you define your List and sort order (asc or dec). It can be as simple or complex you want.
Change the
Ordering
library to theSortOrder
library, referencing this library:import org.richfaces.component.SortOrder;
The sort order comparator can be defined in a variable programmatically using the
<rich:column>
attribute:This is an example of using SortOrder programmatically using JSF 2.x/RichFaces 4.x. It uses a three-state sort method: unsorted (default), ascending, and descending, and implemented by setting the sortOrder attribute.
Or the comparator default behavior can be overridden in code, as in this example:
In your view, you define the which headers you want to sort with, so add a commandLink to make each header clickable.
Now you have to implement the sort for your bean with basic collections, again, it can be as complex as you can:
You can make your life easier by reusing stuff instead of doing that for each column, I will let you figure that out. You can use better libraries for Java to help you do the comparator for example with Guava from Google or Collection Commons from Apache.
Instead of doing all that, and reinventing the wheel, use a framework that abstracted all this out for you, they make your life way easier..
There are several component librarys on top of jsf-2.0.
Primefaces for instance has a very powerful datatable component with sorting / filtering options. Primefaces is very easy to integrate into your project and comes with a lot of themes (or you can create your own theme).
Just have a look at the showcase and the documentation.
Other popular component libraries are
Richfaces(as per comment datatable sorting not supported) and Icefaces.You can do it in Richfaces too. Richfaces 3.3.x supports easy sort:
In Richfaces 4.x you can sort datatable using Sorting bean:
or your DataModel (extends ExtendedDataModel):
You can add arrows (for sorting order displaying) inside in command link and it will be exact same presentation as in Richfaces 3.3.3.
UPDATE
Starting from RichFaces 4.5 there is support of simple sorting in dataTable (like it was in version 3.3.x).