Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

I'd suggest that you retrieve the data you're going to display already sorted. If that's the case you then only have to set the correct sort icon (ascending or descending):

/**
 * Displays the appropriate sorted icon in the header of the column for the given index.
 * 
 * @param columnIndex
 *            of the column to mark as sorted
 * @param ascending
 *            <code>true</code> for ascending icon, <code>false</code> for descending icon
 */
 public void setSortedColumn(int columnIndex, boolean ascending) {
      Column<T, ?> column = table.getColumn(columnIndex);
      if (column != null && column.isSortable()) {
           ColumnSortInfo info = table.getColumnSortList().push(column);
           if (info.isAscending() != ascending) {
                table.getColumnSortList().push(column);
           }
      }
 }

If the data can't be sorted before retrieving you can sort the list the same way you do it when a user clicks the header (onColumnSort(ColumnSortEvent event) with a Comparator) before you display it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top