12

我正在使用 JSF 2.0 和 Primefaces 3.2。我有一个带有数据表的主表单,每行上有一个按钮,它会弹出一个带有另一个数据表的对话框。对话框有自己的形式。在对话框中选择数据表行时,我处理选择,然后在主窗体中刷新数据表。刷新后,我在主数据表上的所有过滤器都丢失了。有没有办法记住过滤器值?

我的 JSF 页面

<h:panelGroup id="mainTable">
<h:form id="mainForm" prependId="false">                
<p:dataTable id="cl_grid1" var="item"    widgetVar="mainGrid"
                value="# {controller.items}">


<p:column headerText="Colum1" filterMatchMode="contains" filterBy="#{item.myObj.code}" sortBy="#{item.myObj.code}">
                        <h:outputText value="#{item.myObj.code}" style="float:left" />

                        <p:commandButton id="selectBtn" icon="select"
                            oncomplete="selectDialog.show()" actionListener="#{controller.setMainItem}">
                            <f:param name="selectedMainItem" value="#{item.id}"/>
                        </p:commandButton>



                </p:column>

</p:dataTable
</h:form>
</h:panelGroup>

<p:dialog id="cl_selectDialog" header="Select AAAA" dynamic="true" modal="true" widgetVar="selectDialog" 
    height="500" width="900" showEffect="explode" hideEffect="explode">

        <h:form id="selectForm" prependId="true">

            <p:dataTable id="cl_selectGrid" var="selectorItem"    widgetVar="selectGrid"
                value="# {controller.selectorItems}" selection="#{controller.selectedItem}" 
rowKey="#{selectorItem.id}" selectionMode="single">



            <p:commandButton actionListener="#{controller.processSelection}" value="Ok" icon="ui-icon ui-icon-search" 
                                    oncomplete="selectDialog.hide()" update=":mainTable" process="@form"/>
            <p:commandButton value="Cancel" icon="ui-icon ui-icon-search" onclick="selectDialog.hide()" type="button"/>
        </h:form>
    </p:dialog>

我的控制器

在我的控制器中,我所做的就是再次为我的数据表补水。

@Component
@Scope("view")
public class Controller extends BaseController implements Serializable {

private List<Item> items;

public Controller() {
    items = new ArrayList<Item>();  
}


@PostConstruct
public void init() {
    loadItems();
}

protected void loadItems() {
    items = //Load items from Database
}


public void processSelection(ActionEvent event) {
    //Add the selected Item and then reload items
    loadItems();
}

}
4

3 回答 3

16

如果是 AJAX 请求,您可以在触发器组件上添加一个“oncomplete”方法,该方法具有:

<p: ... update='myDatatableId' oncomplete='myDatatableWidget.filter();' />

通过这种方式,您将重新触发数据表上的当前过滤器,或者您也可以使用清除所有过滤器

 ... oncomplete='myDatatableWidget.clearFilters();' ...
于 2013-01-04T14:15:54.713 回答
2

我们还提出了一个比 kolossus 发布的更优雅的解决方案,它就像扩展 PF Datatable 以获得持久过滤器值一样简单。

http://www.stickysession.com/?p=258

于 2013-02-20T14:49:28.587 回答
1

Since you must to refresh the table from within its form (so it will preserve the filter state) another solution could be to place a hidden button in the main table form and use it as a 'proxy' by clicking it from your dialog, that way the filter will be preserved, like this

change your button from

 <p:commandButton actionListener="#{controller.processSelection}" value="Ok" icon="ui-icon ui-icon-search" 
     oncomplete="selectDialog.hide()" update=":mainTable" process="@form"/>

into this (just a simple button that clicks the hidden one)

 <p:commandButton onclick="$('#my-proxy-button-id').click(); return false;" value="Ok" icon="ui-icon ui-icon-search"/>

and add a new button inside the main table form

 <p:commandButton style="display:none;" id="my-proxy-button-id" actionListener="#{controller.processSelection}" 
     oncomplete="selectDialog.hide()" update="mainTable" process=":selectForm"/>

using this technique you wont be needed to call for filtering manually and no additional "blinking" will appear on screen

于 2014-09-30T12:34:57.723 回答