DataTable clearFilter() not working properly

前端 未结 4 900
失恋的感觉
失恋的感觉 2021-02-02 14:33

I have a complicate JSF that contain dataTable with filter in each one of the columns. In order to make sure that the generate button will fetch all the data first I need to cle

相关标签:
4条回答
  • 2021-02-02 14:33

    For clearing custom filters you can use primefaces resetInput, along with clearFilters() discussed in other answers, and a custom actionListener method. See code snippets below:

    <p:dataTable id="dataTable" widgetVar="dataTable"
        value="#{bean.listOfObjects}" var="object">
    
    <p:commandButton value="Clear All Filters"
        onclick="PF('dataTable').clearFilters()" 
        actionListener="#{controller.clearAllFilters}"
        update="dataTable">
        <p:resetInput target="dataTable" />
    </p:commandButton>
    

    Controller.java

    public void clearAllFilters() {
    
        DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("form:dataTable");
        if (!dataTable.getFilters().isEmpty()) {
            dataTable.reset();
    
            RequestContext requestContext = RequestContext.getCurrentInstance();
            requestContext.update("form:dataTable");
        }
    }
    

    I hope this helps anyone looking to clear custom filters.

    0 讨论(0)
  • 2021-02-02 14:48

    To clear all the inputs of the filters you can do it by javascript:

    <p:commandButton onclick="PF('vtWidget').clearFilters()" />
    

    vtWidget is the widgetVar of the datatable.

    Basically clearFilters() will clear the fields for you and call filter(), and the filter function would update your datatable, which in turns will empty the filtered list.

    Note: This would work only if the filters were inputText. If you have custom components then you should implement your own clear based on the components that you have.

    Sometimes if you have custom components, you need to empty the filtered list manually, as you did in the comments!

    0 讨论(0)
  • 2021-02-02 14:54

    For Primefaces 7 and above, use the following:

    public void clearAllFilters() {
    
        DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("form:dataTable");
        if (!dataTable.getFilters().isEmpty()) {
            dataTable.reset();
    
            PrimeFaces.current().ajax().update("form:dataTable");
        }
    }
    
    0 讨论(0)
  • 2021-02-02 15:00

    This is how i solved my problem.

     RequestContext requestContext = RequestContext.getCurrentInstance();
     requestContext.execute("PF('widget_orderDataTable').clearFilters()");
    

    Hope its help.

    0 讨论(0)
提交回复
热议问题