0

I have some problems by updating a datatable with a commandButton.

I checked:

Primefaces - update datatable with commandButton doesn't work

but I don't see what is the problem:

I use a commandButton onclick to search with the globalFilter. The lazy loading is ok but the filter only works when I press search button and then, another update button to update de datatable .

The search button filter onclick. And the update button redirect onclick to getPaginator().setPage(0), that shows the update datatable. This is the only way I see to update the datatable

I've try to update form:datatable in comandButton and actionListener to bean.resetDatable() without success.

I need only to press search button and update datatable both together in one button.

<h:form id="myDataForm">
    <p:dataTable id="MyData" rowStatePreserved="true" 
                 var="varMyData" 
                 value="#{MyData.MyDataLazyDataModel}" lazy="true" filterDelay="1000"                                            
                 widgetVar="wMyData" 
                 rowKey="#{MyData.id}" 
                 paginator="true" paginatorPosition="bottom" rows="10"                                             
                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"                                 
                 emptyMessage="No data" >


        <f:facet name="header">
            <p:outputPanel>
                <h:outputText value="Search: " />                

                <p:inputText id="globalFilter" onkeypress="PF('wMyData').filter();" />

                <p:commandButton  value="Search" class="ui-button" icon="fa fa-search"  onclick="PF('wMyData').filter();"                                  
                                  update=":myDataForm:MyData" actionListener="#{MyData.resetDatatable()}"/>                                        
                <p:commandButton value="Update" onclick="VarMyData.getPaginator().setPage(0);"/>                            
            </p:outputPanel>                    
        </f:facet>                       
        <p:column id ="numdoc" headerText="ID number" sortBy="#{varMyData.idNumber}"  
                  filterStyle="display:none" filterBy="#{varMyData.idNumber}"  filterMatchMode="exact" >
            <h:outputText value="#{varMyData.idNumber}" />
       </p:column>
     </datatable>
</form>

I've tried to reset with lazydatamodel to null and PrimeFaces.current(), but nothing.

myDataView.java




    public MyDataLazyDataModel getMyDataLazyDataModel() {

        if (myDataLazyDataModel == null) {
            myDataLazyDataModel= new MyDataLazyDataModel(getListAll());
        }
        return myDataLazyDataModel;
    }

    public void setMyDataLazyDataModelT(MyDataLazyDataModel myDataLazyDataModel) {
        this.myDataLazyDataModel = myDataLazyDataModel;
    }



 public String resetDatatable() {

   //option1: 
    myDataLazyDataModel = null;        

  //option2
    DataTable dataTable = (DataTable)      FacesContext.getCurrentInstance().getViewRoot().findComponent("myDataForm:myData");
    PrimeFaces.current().ajax().update("myDataForm:myData");
    if (!dataTable.getFilters().isEmpty()) {
        dataTable.reset();
        PrimeFaces.current().ajax().update("myDataForm:MyData");

    }

Thanks.

Oscar P
  • 61
  • 2
  • 12
  • 3
    Why do you add a listener on keypress, when you use a button for the search (and without any delay from one call to another)? Are you sure, using that logic, that on click function is done after the completion of actionListener? Do the VarMyData.getPaginator() call really works? Try starting from a basic data table and then add one thing per time, until you get an error (or your goal) check also browser console. – WoAiNii Apr 21 '20 at 17:25
  • @WoAiNii "check also browser console" : Ok, the problem was related with an Spring upgrade and ajax. I debugged and " gives 403 Forbidden" https://www.baeldung.com/spring-security-csrf Now, works fine. – Oscar P Apr 22 '20 at 04:10
  • So you weren't submitting your csrf token with one of both of these button, please then post an answer with what you do to resolve it. – WoAiNii Apr 22 '20 at 06:38
  • 1
    also check https://stackoverflow.com/questions/26969415/should-protected-views-be-used-for-jsf-2-2-csrf-protection – Kukeltje Apr 22 '20 at 08:13
  • 1
    Thanks @OscarP: I added it. – BalusC Apr 22 '20 at 09:53

1 Answers1

1

I checked console and the problem was related with an Spring 3 to Spring 4 upgrade and ajax which "gives 403 Forbidden" without submit crsf token.

After submitting crsf token: https://www.baeldung.com/spring-security-csrf

 <h:head>
.....
<meta name="_csrf" content="#{_csrf.token}"/>
<meta name="_csrf_header" content="#{_csrf.headerName}"/>
....
</h:head>

I created a global filter and now I can filter after press search button or enter key, and the datable is updated.

<h:form id="myDataForm">
    <p:dataTable id="MyData" rowStatePreserved="true" 
                 var="varMyData" 
                 value="#{MyData.MyDataLazyDataModel}" lazy="true" filterDelay="1000"                                            
                 widgetVar="wMyData" 
                 rowKey="#{MyData.id}" 
                 paginator="true" paginatorPosition="bottom" rows="10"                                             
                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"                                 
                 emptyMessage="No data" >


        <f:facet name="header">
            <p:outputPanel>
                <h:outputText value="Search: " />                
                <p:inputText id="globalFilter" onkeypress="if (event.keyCode === 13)  {PF('wMyData').filter(); return false;}"                                               
            placeholder="Enter data and press enter or search button"/>                                    
               <p:commandButton  class="ui-button" icon="fa fa-search"  onclick="PF('wMyData').filter() " update="growl"/>
            </p:outputPanel>                    
        </f:facet>                       
        <p:column id ="numdoc" headerText="ID number" sortBy="#{varMyData.idNumber}"  
                  filterStyle="display:none" filterBy="#{varMyData.idNumber}"  filterMatchMode="exact" >
            <h:outputText value="#{varMyData.idNumber}" />
       </p:column>
     </datatable>
</h:form>

Oscar P
  • 61
  • 2
  • 12
  • A similar question and aswer would be this: https://stackoverflow.com/questions/30133329/spring-security-4-and-primefaces-5-ajax-request-handling – Oscar P Apr 22 '20 at 09:53