I have a Gwt celltable. Clicking on the headers sorts the columns properly. But on page load the columns are not sorted by default. I want to make the right most column to be sorted when the page loads.
To clarify a couple of the existing answers... a cellTable
's sort list (as accessed by the getColumnSortList()
function) only determines how the state of the table's header, but does not actually sort any data.
As @z00bs suggested, it may be wise to sort the data externally, if possible. If you know that the data is going to be pre-sorted, then you should use the getColumnSortList().clear()
and getColumnSortList().push()
functions to communicate to your users how the data is sorted.
If, however, you want the CellTable to actually sort the data, you're going to need to trigger an event to force the CellTable to actually sort the constituent data client-side. To do this, you can use the state ColumnSortEvent.fire()
method, as such:
ColumnSortEvent.fire(myTable, myTable.getColumnSortList());
This will trigger an event which handles the sorting of the data based on the current state of the header. So you could set the desired initial sort state of the header first, and then execute this line to actually make the data ordering reflect the current sort state represented in the header.
You can use the getColumnSortList() and push the column you want to sort by, as such:
dataGrid.getColumnSortList().push(columnToSortBy);
The table will be sorted by the given column in ascending order.
Calling this method twice, will trigger a check to tests if the given column already pushed to the list, and if so it will gets sorted in descending order, so to get the table sorted by the column in descending order use:
dataGrid.getColumnSortList().push(columnToSortBy);
dataGrid.getColumnSortList().push(columnToSortBy);
Behind the scene, The column is pushed to an inner list in the table called ColumnSortList to position 0. The same list is updated on each column header click.
Make sure you call this method after you initialized the column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With