简体   繁体   中英

Ajax update doesn't work, when using filter on p:dataTable

I have a datable which includes the filter feature of primefaces. Some operations can be done on the table (eg edit). The datable will be updated after the user's operation is completed using ajax. It updates the table directly and works well, if I don't filter the datatable, unfortunately not if I use it and edit it.

That's how my datatable looks like:

    <p:dataTable id="dataTable" var="row"
                value="#{bean.value}"
                filteredValue="#{bean.filteredValue}"
                paginator="true" rows="25" paginatorPosition="bottom"
                rowKey="${row.id}"
                paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                editable="true">

and the Button which triggers the update

<p:commandButton value="Save"
                        actionListener="#{bean.save}"
                        update=":form"/>

After updating datatable you have to invoke it's client side filter() method.

<p:dataTable widgetVar="dataTableWidgetVar" id="dataTable" var="row"
             value="#{bean.value}"
             filteredValue="#{bean.filteredValue}"
             paginator="true" rows="25" paginatorPosition="bottom"
             rowKey="${row.id}"
             editable="true">

<p:commandButton value="Save"
                 actionListener="#{bean.save}"
                 update=":form"
                 oncomplete="PF('dataTableWidgetVar').filter()"/>

For PrimeFaces versions older than 5, you should use

<p:commandButton value="Save"
                 actionListener="#{bean.save}"
                 update=":form"
                 oncomplete="dataTableWidgetVar.filter()"/>

Adding answer for Primefaces 5.x , since they changed the way for calling widget var:

Almost same as Kerem Baydogan's answer but with a little modification :

<p:commandButton value="Save"
                 actionListener="#{bean.save}"
                 update="@form"
                 oncomplete="PF('dataTableWidgetVar').filter()"/>

Or you can clear filters at all with :

<p:commandButton value="Save"
                 actionListener="#{bean.save}"
                 update="@form"
                 oncomplete="PF('dataTableWidgetVar').clearFilters()"/>

For the version of primefaces greater or equal to 5, you can use this block of code, it works very well

<p:dataTable var="page" value="#{yourBean.allData}" widgetVar="yourWidgetVarName"/>

<p:commandButton value="delete"
                 actionListener="#{yourBean.delete}"
                 update="@form"
                 oncomplete="PF('yourWidgetVarName').filter()"/>

如果您使用的是primefaces的第5版,则只需创建表示单个数据行的数据类,即可实现Serializable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM