Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear global filter in PrimeFaces DataTable

Tags:

jsf

primefaces

How can I clear global filter in PrimeFaces DataTable using "Clear" CommandButton? I found similar question on PrimeFaces forum, but looks like not answered - the tip one can find there didn't help me. I tried to solve the problem as follows:

<p:dataTable id="myTab" widgetVar="myTabWidgetVar" var="obj"
    value="#{managedBean.objects}">
    <f:facet name="header">
        <p:outputPanel>
            <h:outputText value="Search all fields:"/>
            <p:inputText id="globalFilter" onkeyup="myTabWidgetVar.filter()"/>
        </p:outputPanel>
    </f:facet>
</p:dataTable>
<p:commandButton value="Clear" onclick="myTabWidgetVar.clearFilters();"
    update="@form"/>

It clears column filters only. Value in global filter remains uncleared. Can you help me to solve the problem?

like image 968
Serg Avatar asked Dec 20 '22 09:12

Serg


2 Answers

Here is how:

$("#someFormId\\:myTab\\:globalFilter").val("").keyup();//if you got `prependId="false" , than omit the `someFormId\\:` part from the selector and leave only `myTab\\:globalFilter`

This will fill your filter with empty string and trigger a keyup event , resulting in clearing your filter and clearing the filter state of the table

If you just want to clear the filter without resetting the filter applied to your table use

$("#someFormId\\:myTab\\:globalFilter").val("");

If you want to use this code in a commandButton there is no need to use ajax (update...)

Just use onclick like this

<p:commandButton value="Clear" 
onclick="$('#someFormId\\:myTab\\:globalFilter').val('').keyup(); return false;"/>

use return false; so the button wont submit your page...

like image 87
Daniel Avatar answered Jan 04 '23 07:01

Daniel


<pdataTable  widgetVar="departments" />
<p:commandButton value="#{lang:text('Add department')}"
                 id="....."
                 actionListener="....."
                 update="@form"
                 oncomplete="PF('departments').clearFilters()"
                 styleClass="........."/>

It works in my case for prime faces

like image 43
user4217506 Avatar answered Jan 04 '23 06:01

user4217506