Gwt celltable调用列的排序

Har*_*eet 16 sorting gwt celltable

我有一个Gwt celltable.单击标题可以正确排序列.但是在页面加载时,默认情况下不对列进行排序.我想在页面加载时使最右边的列排序.

Jef*_*len 27

为了澄清一些现有的答案......一个cellTable排序列表(由getColumnSortList()函数访问)只确定表头的状态,但实际上并没有对任何数据进行排序.

正如@ z00bs建议的那样,如果可能的话,在外部对数据进行排序可能是明智之举.如果您知道数据将被预先排序,那么您应该使用getColumnSortList().clear()getColumnSortList().push()函数来向用户传达数据的排序方式.

但是,如果您希望CellTable实际对数据进行排序,则需要触发事件以强制CellTable实际对客户端组成数据进行排序.为此,您可以使用state ColumnSortEvent.fire()方法,如下所示:

ColumnSortEvent.fire(myTable, myTable.getColumnSortList());
Run Code Online (Sandbox Code Playgroud)

这将触发一个事件,该事件根据标头的当前状态处理数据的排序.因此,您可以先设置标题的所需初始排序状态,然后执行此行以实际使数据排序反映标题中表示的当前排序状态.


Gal*_*cha 11

您可以使用getColumnSortList()并按下要排序的列,如下所示:

dataGrid.getColumnSortList().push(columnToSortBy);
Run Code Online (Sandbox Code Playgroud)

该表将按给定列按升序排序.

调用此方法两次,将触发检查以测试给定列是否已经推送到列表,如果是,它将按降序排序,因此要按照降序排列表,请使用:

dataGrid.getColumnSortList().push(columnToSortBy);
dataGrid.getColumnSortList().push(columnToSortBy);
Run Code Online (Sandbox Code Playgroud)

在场景后面,该列被推送到名为ColumnSortList的表中的内部列表到位置0.在每个列标题点击上更新相同的列表.

确保在初始化列后调用此方法.

  • 我正在连续两次调用push(),但是,虽然箭头显示它排序两次,但实际数据不是.想不通为什么会这样. (4认同)
  • 我遇到了这个问题,并且只是将解决方案作为替代答案发布. (2认同)

z00*_*0bs 7

我建议你检索你要显示的数据已经排序.如果是这种情况,那么您只需要设置正确的排序图标(升序或降序):

/**
 * 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);
           }
      }
 }
Run Code Online (Sandbox Code Playgroud)

如果在检索之前无法对数据进行排序,则可以在用户在显示之前单击标题(onColumnSort(ColumnSortEvent event)带有a Comparator)时对列表进行排序.