1

在过去的几天里,我面临着一项对我来说成为问题的任务。

我正在使用rich:dataTable自己的过滤和排序列。在后端使用标准排序和过滤 bean 进行简单输入或选择。我的问题是,我需要以某种方式记住许多表单的排序和过滤值,以便在某些情况下恢复它们 - 例如:用户使用后退按钮(最重要的情况)。我知道如何处理浏览器后退按钮,但我不知道必须以某种简单明了的方式保存和恢复我的值。重要的是我不能使用 rich:extandedDataTable并且我使用 bean 的视图范围。

(其中一种解决方案是使用会话范围 bean 来管理 s&f,但是为一种形式制作一个 bean 成本太高,而且制作一个这样的 bean 以我想使用的方式使用起来非常复杂。)

所以,我的问题是:我该怎么做?处理这种事情的最佳做法是什么?我应该走哪条路?

我正在使用 RF 4.3 和 Mojarra 2.1.17(我认为这并不重要)。

4

1 回答 1

2

理想情况下,该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
        }
    
于 2013-02-27T16:50:14.813 回答