Monday, February 16, 2009

Pagination with rich:subTable & rich:dataTable

Ok.. I could now get the sorting working with dataTable and subTable in my seam application. I have one last problem to tackle , how can I do pagination with data displayed from multiple subTables ? The rich:datascroller does a decent job of providing an automatic pagination if we provide it with a dataModel, but I am struck with a dilemma.. before going into details further, lets look at the following images.



when clicked on the button in the first column the row expands revealing the detail rows which are implemented using subTable

After expanding

The dilemma I am talking about is , which table-id should i give to datascroller for handling pagination? of course the first (main) table's table id, as we want to display the main table's rows and let the user click on the rows to reveal the detail rows. But imagine if we have another level of grouping which effectively calls us to use three sub tables, and we want to paginate on the second grouping level, how shall we handle this ?

I quickly realized that it is not possible for a single datascroller to handle this because the total number of rows to be displayed on a page are going to come from multiple parent rows , hence multiple subTable id's and multiple datascroller components !!! I know its not a clean way...

The cleaner way is to somehow leverage the dataTable components properties which allows us to control the number of rows to display (these are the same attributes that are used by datascroller component to achieve pagination)

Similar to external pagination which we used to implement pagination with dataTable and subTable , I have to come up with a helper which determines how many rows each dataTable and subTable component shall display to simulate a pagination usecase.

incase if you have not familiar with how datascroller component achieves pagination try this on your dataTable




#{detail.name}


#{detail.place}




The attribute "first" indicates the first row to display and the "rows" attribute the number of rows. As you might have guessed the above snippet displays the page "2" .

I will have to use the above scheme to simulate pagination across multiple subTables

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

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.