Programatic CellTable sort in GWT not working
Asked Answered
A

2

5

I'm using the ListDataProvider example here as a guide. The columns are sorting fine as expectd based on the provided comparators. I'm trying to programatically apply a sort as alluded to on this line from the example:

// We know that the data is sorted alphabetically by default.
table.getColumnSortList().push(nameColumn);

What this does, is it makes the cell column appear to be sorted with the carrot sort indicator. However, the underlying data isn't sorted. Is there a way to get the table to actually apply the sort progarmatically. I suppose I could use this in conjunction with actually sorting the data via Collections.sort(), but I'd like to avoid that and do it in one place.

Ambur answered 6/12, 2012 at 21:11 Comment(0)
M
10

You can apply sorting on a column programatically with little exta code. The following code snippet does that -

When ever u set data to the cellTable you have to initialize the ListHandler as below code does -

cellTable.addColumnSortHandler( createColumnSortHandler() );

private ListHandler<T> createColumnSortHandler()
{
     final ListHandler<T> listHandler = new ListHandler<T>(listDataProvider.getList());
     listHandler.setComparator( sortColumn, comparator );
     return listHandler;
}

And when u want to fire the SortEvent execute the following code snippet -

ColumnSortInfo columnSortInfo = new ColumnSortInfo( sortColumn, sortDirection );
cellTable.getColumnSortList().push( columnSortInfo );
ColumnSortEvent.fire( cellTable, cellTable.getColumnSortList());
Methanol answered 7/12, 2012 at 16:31 Comment(1)
My missing ingredient was ColumnSortEvent.fire(). Did the trick.Ambur
P
0

you have to call setData on grid again.....

Paediatrician answered 7/12, 2012 at 2:24 Comment(2)
This doesn't work, nor did I expect it to. I'm not ever sorting the underlying list, just pushing the column on to the sort list.Ambur
The sorting has implicitly been set. Much easier to call: ColumnSortEvent.fire( cellTable, cellTable.getColumnSortList());Caryloncaryn

© 2022 - 2024 — McMap. All rights reserved.