Gwt celltable调用列的排序

时间:2011-11-18 02:10:04

标签: gwt sorting celltable

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

3 个答案:

答案 0 :(得分:26)

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

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

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

ColumnSortEvent.fire(myTable, myTable.getColumnSortList());

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

答案 1 :(得分:12)

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

dataGrid.getColumnSortList().push(columnToSortBy);

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

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

dataGrid.getColumnSortList().push(columnToSortBy);
dataGrid.getColumnSortList().push(columnToSortBy);

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

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

答案 2 :(得分: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);
           }
      }
 }

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

相关问题