我有一个 Gwt 单元格表。单击标题可以正确地对列进行排序。但是在页面加载时,默认情况下不会对列进行排序。我想在页面加载时对最右边的列进行排序。
3 回答
为了澄清几个现有的答案...... acellTable
的排序列表(由getColumnSortList()
函数访问)仅确定表头的状态,但实际上并不对任何数据进行排序。
正如@z00bs 建议的那样,如果可能的话,在外部对数据进行排序可能是明智的。如果您知道数据将被预先排序,那么您应该使用getColumnSortList().clear()
和getColumnSortList().push()
函数向您的用户传达数据是如何排序的。
但是,如果您希望 CellTable 对数据进行实际排序,则需要触发一个事件来强制 CellTable 对客户端的组成数据进行实际排序。为此,您可以使用 stateColumnSortEvent.fire()
方法,如下所示:
ColumnSortEvent.fire(myTable, myTable.getColumnSortList());
这将触发一个事件,该事件根据标题的当前状态处理数据的排序。因此,您可以先设置标题的所需初始排序状态,然后执行此行以实际使数据排序反映标题中表示的当前排序状态。
您可以使用getColumnSortList()并推送要排序的列,如下所示:
dataGrid.getColumnSortList().push(columnToSortBy);
该表将按给定列按升序排序。
调用此方法两次,将触发检查以测试给定列是否已推送到列表,如果是,它将按降序排序,因此要按列按降序排序表,请使用:
dataGrid.getColumnSortList().push(columnToSortBy);
dataGrid.getColumnSortList().push(columnToSortBy);
在幕后,该列被推送到表中名为 ColumnSortList 的内部列表到位置 0。每次单击列标题时都会更新相同的列表。
确保在初始化列后调用此方法。
我建议您检索要显示的已排序的数据。如果是这种情况,您只需设置正确的排序图标(升序或降序):
/**
* 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)
带有 a )时所做的相同。Comparator