Sunday, January 18, 2009

External Sorting in seam application with rich:dataTable

When working with seam application you may have encountered a scenario where you have to implement rich:dataTable in a non-conventional way , for example look at the below fragment


NORMAL WAY





Symbol

#{aRow.symbol}




Quantity

#{aRow.quantity}


...
..
A MORE FLEXIBLE WAY..






Symbol
Quantity



#{aRow.symbol}



#{aRow.quantity}




notice the second case, that we are not defining the table headers with "header" facet for each column, instead we are defining all the header columns using a single header facet. This allows me to do stuff like detail rows for each row using subtable and columngroups easily. This approach brings a problem too, it is not easy to define sorting information to take advantage of automatic sorting feature of dataTable. Rich faces demo gives an nice example of using external sorting, but it may not be very obvious on how to implement sorting by clicking on header rows, atleast for me it was little difficult to get around this problem. Actually it is not that difficult , here is how hey do it....

1. We need to make use of dataTable's sortPriority property to tell dataTable which column the table should be sorted on.
2. Define IDs for all the columns you want to provide sorting ability
3. Make the header clickable and on click event change the sort priority (dont worry if this is not clear, I will explain you in detail.. shortly)


Because we need to use the same feature for many screens , we will not write anything in our conversation scoped sessionbeans , instead we will define a reusable bean which can store the sortPriority and sortOrder , as per rich faces documentation I named the bean SortingBean, less surprising. huh..

it is a simple bean which holds the current sort column and sortingOrder (ASCENDING or DESCENDING), here is the class

package com.dezinx.shareguru.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SortingBean implements Serializable {

/**
*
*/
private static final long serialVersionUID = 3478316227248532205L;

private String sortingOrder;
private List sortingPriority;

public SortingBean() {
sortingOrder = "ASCENDING";
sortingPriority = new ArrayList();
}

public void sortOn(String columnName) {
if (sortingPriority.contains(columnName)) {
toggleSortingOrder();
} else {
sortingPriority.clear();
sortingPriority.add(columnName);
sortingOrder = "ASCENDING";
}
}

private void toggleSortingOrder() {
if (sortingOrder.equals("ASCENDING"))
sortingOrder = "DESCENDING";
else
sortingOrder = "ASCENDING";
}

public String getSortingOrder() {
return sortingOrder;
}

public void setSortingOrder(String sortingOrder) {
this.sortingOrder = sortingOrder;
}

public List getSortingPriority() {
return sortingPriority;
}

public void setSortingPriority(List sortingPriority) {
this.sortingPriority = sortingPriority;
}
}


Now we have to expose this sortingBean to the view and it can be easily done by outjecting the instance of SortingBean , like below, in your seam action (stateful session bean)


.....
@Out(scope=ScopeType.CONVERSATION)
SortingBean sortingBean;
...




Finally , lets define the view


..





Price



Quantity




#{aRow.price}



#{aRow.quantity}





in the above code, take notice of how we set selfSorted property to false , defined column ids to refer in a:support , which fires an ajax request to set the current column to sort, Also notice that a:support has reRender attribute set to the id of dataTable, which will force a table rerender after click on table header.

Hope this helps.

4 comments:

Anonymous said...

I am facing the same problem. I am using subtable and columnGroup with the .... The build in client side sorting of datatable does not work. The sorting icon does not show at all. I tried your way based on your code snippet, but it does not seem working. I wonder if you can email me your full code, including the seam components and your xhtml. Somehow it keeps saying that SortingBean is null.

Anonymous said...

Or could you post your full code here.

Anonymous said...

I could not get your code working. Could you email me your source code (both java and xhtml)?
My email is ryang5@yahoo.com
Thanks.

Harsha said...

Create the soringBean instance in method annotated with @create

@Create
public void init() {
sortingBean=new SortingBean();
}