jsfprimefaceslazy-loadingprimefaces-datatable

Why filters are becoming null after sorting any field Prime Faces data table with lazy loading?


I searched many question and many and tried out many things. Below I have snippet steps to produce issue. first apply a filter value any column then apply a sort to any column filter value present at table interface but at load method filters.isEmpty() is true.

                         <p:dataTable id="tbl" value="#{service}"
                                         selection="#{bean.selectedObj}" 
                                         filteredValue="#{bean.listFilteredObj}" 
                                         var="obj"
                                         multiViewState="true"  
                                         rows="50"
                                         widgetVar="tbl1" filterEvent="keyup"
                                         draggableColumns="true"
                                         emptyMessage="No Obj found"
                                         paginator="true"  
                                         paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                         rowsPerPageTemplate="50,100" 
                                         paginatorAlwaysVisible="true"
                                         rowKey="#{opp.oppId}"
                                         tableStyle="table-layout:auto;" resizableColumns="true" resizeMode="expand"
                                         rowSelectMode="add"
                                         lazy="true"
                                         rowIndexVar="index"
                                         class="fixed-scrollbar"
                                         style="margin: -10px!important; height: 85vh">
                                <p:ajax event="rowSelectCheckbox" listener="#{service.onRowSelectCheckbox}" update=":dtForm:tbl:btnClose :dtForm:tbl:btnUpdate" />
                                <p:ajax event="rowUnselectCheckbox" listener="#{service.onRowUnselectCheckbox}" update=":dtForm:tbl:btnClose :dtForm:tbl:btnUpdate" />
                                <p:ajax event="toggleSelect" listener="#{service.onToggleSelect}"  update=":dtForm:tbl:btnClose :dtForm:tbl:btnUpdate"  />
                                <p:ajax event="page" listener="#{objFilter.onPageChange}" /> 

                                <p:ajax event="page" listener="#{service.onPagination}" update=":dtForm:tbl"/>  
                                <p:ajax event="colReorder" listener="#{service.onColumnReorder}"/>
                               
                                <p:column exportable="false" toggleable="false" selectionMode="multiple" style="width: 10px;text-align: center;"/>

                                <p:column headerText="Id"  style="display: none" exportable="true"  visible="false" toggleable="false">
                                    <p:outputLabel value="#{obj.Id}"  />
                                </p:column>
                                <c:forEach var="column" items="#{service.listOppColumns}">
                                   
                                    <p:column headerText="#{column.columnHeader}" id="#{column.columnName}"  
                                              visible="#{column.columnVisibleFlag==1}"  
                                              filterMatchMode="#{column.columnFilterMatchMode}" 
                                              filterBy="#{column.columnName=='insDate' || column.columnName=='updDate' ? globalParams.formatDateTime(obj[column.columnName], 'dt') : column.columnName=='oppFollowUp' ? globalParams.formatDateTime(obj[column.columnName], 'da') : opp[column.columnName]}" 
                                              sortBy="#{obj[column.columnName]}" exportable="#{column.columnVisibleFlag==1}"
                                              field="#{column.columnName}">   
                                        <c:choose>
                                           
                                            <c:when test="#{column.columnName=='objFol'}">
                                                <h:link outcome="ObjView.xhtml?id=#{obj.Id}" value="#{globalParams.formatDateTime(opp[column.columnName],'da')}">
                                                    <f:param name="frm" value="#{view.viewId}" />
                                                    <f:param name="moduleId" value="0" />
                                                </h:link>
                                            </c:when>
                                            <c:when test="#{column.columnName=='objDate'}">

                                                <h:link outcome="objView.xhtml?id=#{obj.objId}" value="#{globalParams.formatDateTime(obj[column.columnName],'da')}">
                                                    
                                                    <f:param name="frm" value="#{view.viewId}" />
                                                    <f:param name="moduleId" value="0" />
                                                </h:link>
                                            </c:when>
                                            <c:when test="#{column.columnName=='objPro'}">

                                                <h:link outcome="ObjView.xhtml?id=#{obj.Id}" value="#{globalParams.formatDateTime(opp[column.columnName],'da')}">
                                                    
                                                    <f:param name="frm" value="#{view.viewId}" />
                                                    <f:param name="moduleId" value="0" />
                                                </h:link>
                                            </c:when>
                                            <c:when test="#{column.columnName=='insDate'}">
                                                <h:link outcome="ObjView.xhtml?id=#{obj.Id}" value="#{globalParams.formatDateTime(opp[column.columnName],'dt')}">
                                                    
                                                    <f:param name="frm" value="#{view.viewId}" />
                                                    <f:param name="moduleId" value="0" />
                                                </h:link>
                                            </c:when>
                                            
                                            <c:when test="#{column.columnName=='updDate'}">
                                                <h:link outcome="OpportunityView.xhtml?opid=#{opp.oppId}" value="#{globalParams.formatDateTime(opp[column.columnName],'dt')}">
                                                    <f:param name="frm" value="#{view.viewId}" />
                                                    <f:param name="moduleId" value="0" />
                                                </h:link>
                                            </c:when>
                                            <c:otherwise>
                                                <h:link outcome="ObjView.xhtml?id=#{obj.Id}"    
                                                        value="#{obj[column.columnName]}"  >
                                                    <f:param name="frm" value="#{view.viewId}" />
                                                    <f:param name="moduleId" value="0" />
                                                </h:link>
                                            </c:otherwise>
                                        </c:choose>   
                                    </p:column>
                                </c:forEach> 
                            </p:dataTable>

service.java

...
...
...
 @Override
    public List<ViewObjList> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        globalFlt = new StringBuilder("");
        StringBuilder order = new StringBuilder("");
        StringBuilder tableFlt;
        tableFlt = tableFlt(filters);
        System.out.println("\nfilter is null = "+filters.isEmpty());
        if (sortField != null) {
            if (!sortField.trim().equals("")) {
                order.append(sortField);
                if (sortOrder.equals(SortOrder.DESCENDING)) {
                    order.append(" DESC");
                }
            }
        } else {
            order.append("objFol DESC");
        }

        obj.list = lazyQueryExecute(first, pageSize, globalFlt.toString(), tableFlt.toString(), order.toString());
        obj.listFiltered = obj.list;
        return obj.listFiltered;
    }

    private List<ViewOppList> lazyQueryExecute(int first, int pageSize, String globalFlt, String tableFlt, String order) {
        StringBuilder queryList = new StringBuilder("from ViewOppList ");
       
        String and = "and (";
        queryList.append("where (").append(DepList()).append(") ");

        if (!tableFlt.trim().equals("")) {
            queryList.append(and).append(tableFlt).append(") ");
        }

        if (!globalFlt.trim().equals("")) {
            queryList.append(and).append(globalFlt).append(") ");
        }

        String rowCount = "select count(*) " + queryList;
        long rowC = (long) dao.qryObject(rowCount);
        setRowCount((int) rowC);

        if (!order.trim().equals("")) {
            queryList.append(" order by ").append(order);
        }

        showTotal.append(queryList);
        opp.setOppSubTotal((BigDecimal) dao.qryObject(showTotal.toString()));
        System.out.println("query = " + queryList);
        return dao.qryListMaxLim(queryList.toString(), Dao.query.HQL, first, pageSize);
    }

    @Override
    public ViewObjList getRowData(String rowKey) {
        if (obj.listFiltered != null) {
            for (ViewObjList objo : obj.listFiltered) {
                if (objo.getId() == Integer.parseInt(rowKey)) {
                    return objo;
                }
            }
        }

        return null;
    }
    @Override
    public String getRowKey(ViewObjList obj) {
        return String.valueOf(obj.getId());
    }

private StringBuilder tableFlt(Map<String, Object> filters) {
        StringBuilder tableFlt = new StringBuilder("");
        if (!filters.isEmpty()) {
            for (Map.Entry<String, Object> entry : filters.entrySet()) {
                String field = entry.getKey();
                String value = (String) entry.getValue();
                if (!field.trim().equals("") && !value.trim().equals("")) {
                    if (field.equalsIgnoreCase("globalFilter")) {
                        globalFlt.append(globalSearch(value));
                    } else {
                        // colmn LIKE '%value% and column1 Like '%value1%'
                        tableFlt.append(field).append(" LIKE '%").append(value).append("%' and ");
                    }
                    System.out.println("\n\n\n field = " + field + ": vlue = " + value);
                }
            }
            if (tableFlt.length() > 3) {
                tableFlt.delete(tableFlt.length() - 4, tableFlt.length());
            }
        }
        return tableFlt;
    }

I showed only the required small snippet to reproduce problem. In this code first we apply filter that will show the proper query list (filters.isEmpty() = false) but when sorting applied from table at load method after logging the filters.isEmpty() = true even filtered value present at data table. I am using prime faces version 6.2 and jsf 2.2 and using hql queries to get data from MySQL data table

Any help will be appreciated Thanks in advance.


Solution

  • after spending days on this issue I got that there are some nested form which is causing this issue the above code is okay and I am keeping this question as well so that anyone else should not go through this type of problem. JSF not allowing nested forms need to show that error.