Gwt celltable invoke sort on a column
Asked Answered
S

3

16

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.

Speller answered 18/11, 2011 at 2:10 Comment(0)
F
11

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.

Foresee answered 23/1, 2012 at 17:32 Comment(3)
I am calling push() twice in a row as shown but, although the arrow shows it as sorted twice, the actual data isn't. Can't think why this is happening.Muddler
I had that problem to and just posted the solution as an alternative answer.Noway
jeff allen's solution to fire the event is what's missing in this answer.Normalie
N
27

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.

Noway answered 9/8, 2012 at 21:31 Comment(3)
I used this answer to help with re-sorting column after applying certain filters on the columns. Thanks!Kutzenco
Oooooooooooooooooooooooh I thought I was going crazy, thanks :)Zach
Nearly 2020 and this solution still works perfectly :-)Outspoken
F
11

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.

Foresee answered 23/1, 2012 at 17:32 Comment(3)
I am calling push() twice in a row as shown but, although the arrow shows it as sorted twice, the actual data isn't. Can't think why this is happening.Muddler
I had that problem to and just posted the solution as an alternative answer.Noway
jeff allen's solution to fire the event is what's missing in this answer.Normalie
K
7

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.

Kalina answered 18/11, 2011 at 7:33 Comment(1)
This was useful to ensure that the default sorting didn't cycle between ascending and descending when using Jeff Allen's solution.Feodora

© 2022 - 2024 — McMap. All rights reserved.