2013-02-25 44 views
0

過去幾天我面臨着一個對我來說成爲問題的任務。保存和恢復排序dataTable的輸入值的最佳方法(JSF,Richfaces)

我使用rich:dataTable與自己的篩選和排序列。簡單的輸入或選擇與標準的排序和過濾豆後端。我的問題是,我需要記住這種排序和篩選值的許多形式來恢復它們在某些情況下 - 例如:用戶使用後退按鈕(最重要的情況下)。我知道如何處理瀏覽器後退按鈕,但我不知道必須以一種簡單明瞭的方式保存和恢復我的值。重要的是我不能使用rich:extandedDataTable我使用bean的視圖範圍。

(其中一個解決方案是使用會話作用域bean來管理s,但是爲一個表單創建一個bean遠遠很貴,並且使得一個這樣的bean非常複雜,以我想要的方式使用它)

所以,我的問題是:我該怎麼做?處理這樣的事情的最佳做法是什麼?我應該走哪條路?

我正在使用RF 4.3和Mojarra 2.1.17(我認爲這並不重要)。

回答

1

理想情況下,stateVar屬性對於您的需求是理想的,但其上的文檔很少,而且似乎沒有人真正知道如何處理它。我會推薦以下hack,基本上你可以手工保存和恢復數據表變量的狀態

如果你只是想保留表的當前過濾狀態,RF datatable有一個getComponentState()方法,那。如果你想要存儲特定的值,你必須自己深入數據表。無論你選擇,你必須做的某個時候它在組件的生命週期

  1. 定義合適的<f:event/>聽衆中,你將捕獲的數據表中的狀態變量。我推薦preValidate

    <rich:extendedDataTable binding="#{bean.table}" ...>  
        <f:event type="preValidate" listener="bean.saveTableState"/>   
    </rich:extendedDataTable> 
    

    然後定義您的支持bean的方法,將檢索表中的狀態變量綁定

    public void saveTableState(ComponentSystemEvent evt){ 
         UIExtendedDataTable table = (UIExtendedDataTable)evt.getComponent(); 
         //now you have the table, you can get what you need from it 
         DataComponentState savedState = table.getComponentState(); //this object obtained here you can restore to reset the table to it's condition when you obtained the state. 
         //or go into the table's hierarchy to retrieve specific values 
         Iterator<UIComponent> cols = table.columns(); 
         while(cols.hasNext()){ 
         UIColumn col = (UIColumn)cols.next(); 
          col.getFilterValue(); //Retrieve the current filter value on the column 
         }  
    } 
    
  2. 根據您的喜好,找到在組件的生命週期中合適的點來恢復值。我會推薦preRenderComponent

    public void restoreTableConditions(ComponentSystemEvent evt){ 
        table.restoreState(FacesContext.getCurrentInstance(),savedState); //restore the DataComponentState from wherever you stashed it 
        } 
    
+0

謝謝您的回覆!這似乎相當不錯。目前我正在研究我之前提到過的解決方案(s&f的一個會話bean),所以如果我沒有成功,我會試試你的方式並報告進度。 但仍然,再次感謝你。我一直在尋找這樣的解決方案,但沒有效果。 – 2013-02-28 07:53:57