2011-11-18 59 views
16

我有一個Gwt celltable。點擊標題將列正確排序。 但在頁面加載時,默認情況下不對列進行排序。 我想在頁面加載時使最右邊的列進行排序。Gwt celltable在列上調用排序

回答

12

可以使用getColumnSortList(),推動要排序的列,因爲這樣的:

dataGrid.getColumnSortList().push(columnToSortBy); 

該表將按照升序給定的列進行排序。

調用這個方法兩次,將觸發一個檢查來測試給定的列是否已經推送到列表中,如果是的話,它將按照降序排列,以便按降序排列列表使用:

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

幕後,將柱推到稱爲ColumnSortList表格中的內部列表至位置0的同一列表被更新每個列標題點擊。

確保在初始化列後調用此方法。

+4

我如圖所示連續調用兩次push(),但是,雖然箭頭顯示它已排序兩次,但實際數據卻不是。想不到爲什麼會發生這種情況。 – slugmandrew

+0

同樣在這裏,我不知道爲什麼 –

+2

我有這個問題,只是發佈解決方案作爲替代答案。 –

6

我建議你檢索你要顯示的數據已經排序。如果是這樣的話,你那麼只需要設置正確的排序圖標(上升或下降):

/** 
* 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)。

+0

您的代碼非常有幫助。謝謝。 – slugmandrew

+0

這對於確保默認排序在使用Jeff Allen解決方案時不會在升序和降序之間循環很有用。 –

24

澄清夫婦現有的答案......一個cellTable的排序列表(由getColumnSortList()功能訪問)的唯一決定了表格標題的狀態,但實際上並沒有任何數據進行排序。

正如@ z00bs建議的那樣,如果可能的話,在外部對數據進行排序可能是明智的。如果您知道數據將被預先排序,那麼您應該使用getColumnSortList().clear()getColumnSortList().push()函數來向用戶傳達數據如何排序。

但是,如果您希望CellTable對數據進行實際排序,則需要觸發事件來強制CellTable對組成數據客戶端進行實際排序。要做到這一點,您可以使用狀態ColumnSortEvent.fire()方法,因爲這樣的:

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

這將觸發該處理基於頭的當前狀態的數據排序的事件。因此,您可以首先設置標題的所需初始排序狀態,然後執行此行以實際使數據排序反映標題中表示的當前排序狀態。

+1

我使用此答案來幫助在列上應用某些過濾器後對列進行重新排序。謝謝! – Devesh

+0

Oooooooooooooooooooooooh我以爲我瘋了,謝謝:) – Manu