0

I have huge records and so i am using primefaces lazyloading to fetch the records from database and display, I have implemented load method of lazyDataModel and pagination is working, but while sorting it is not able to sort it properly, my code is as follows:

LazyDataModel<StaffMsgingFltDisplayBean> defaultFlightProcesses = new LazyDataModel<StaffMsgingFltDisplayBean>() {

            @Override
            public List<StaffMsgingFltDisplayBean> load(int first, int pageSize,
                    String sortField, SortOrder sortOrder, Map<String, String> filters) {
                List<CnsFlightProcess> defaultFlightProcessLazyList = null;
                List<StaffMsgingFltDisplayBean> displayBeans = null;
                try
                {
                  defaultFlightProcessLazyList = oneFlightMessageService.retrieveLast24hrsFlightLazy(first,pageSize,sortField,sortOrder);
                  displayBeans = processFltProcess(defaultFlightProcessLazyList);
                  int totalCount  = oneFlightMessageService.retrieveLast24hrsFlightLazyTotalCount();
                  setRowCount(totalCount);
                }
                catch(CNSDBException e)
                {
                    e.printStackTrace();
                }

                return displayBeans;
            }

And my main query code is :

Session session = this.getHibernateTemplate().getSessionFactory()
            .getCurrentSession();
    Transaction transaction = session.beginTransaction();
    Query query = null;
    //String sortOrderValue="desc";
    if(sortOrder.equals(SortOrder.ASCENDING))
    {
        query = session.getNamedQuery("retrieveLast24HrsFlightsLazyAsc");
    }
    else
    {
        query = session.getNamedQuery("retrieveLast24HrsFlightsLazyDesc");
    }


    query.setFirstResult(first);
    query.setMaxResults(pageSize);

    query.setParameter("fromDate", fromDate);
    query.setParameter("toDate", toDate);

    String sortFieldValue = retrieveSortField(sortField);


    query.setParameter("sortField", sortFieldValue);
    //query.setParameter("sortOrder", sortOrderValue);

    List<CnsFlightProcess> flightProcesses = query.list();
    transaction.commit();

Query is :

<query name="retrieveLast24HrsFlightsLazyDesc">
    <![CDATA[from CnsFlightProcess cfp where (cfp.crtDttime between :fromDate and :toDate) and (cfp.fltProcStatus = 'SENT' or cfp.fltProcStatus = 'PROCESSED') order by :sortField desc ]]>
</query>

<query name="retrieveLast24HrsFlightsLazyAsc">
    <![CDATA[from CnsFlightProcess cfp where (cfp.crtDttime between :fromDate and :toDate) and (cfp.fltProcStatus = 'SENT' or cfp.fltProcStatus = 'PROCESSED') order by :sortField ]]>
</query>

Xhtml code:

<p:dataTable styleClass="list" var="flight" rowIndexVar="index" paginator="true" paginatorPosition="bottom" lazy="true" paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}" rows="2"
        rowStyleClass="#{(index % 2) eq 0? 'tablerow' : 'tablerowAlter'}"
        value="#{staffMsgingHandler.flightMessageBean.fltDisplayBeansLazy}"
        width="1100">

        <p:column>
            <f:facet name="header">
                <h:outputText
                            value="#{message['TokenNo']}"></h:outputText>
            </f:facet>
            <h:outputText value="#{flight.flightProcess.reqTokenNbr}"></h:outputText>
        </p:column>
        <p:column sortBy="#{flight.fltNumber}">
            <f:facet name="header">
                <h:outputText
                    value="#{message['FlightNumber']}"></h:outputText>
            </f:facet>
            <h:outputText value="#{flight.fltNumber}"></h:outputText> 
        </p:column>


        <p:column sortBy="#{flight.flightProcess.cnsFlightMstr.fltBpCd}">
            <f:facet name="header">
                <h:outputText
                    value="#{message['FromPoint']}"></h:outputText>
            </f:facet>
            <h:outputText value="#{flight.flightProcess.cnsFlightMstr.fltBpCd}"></h:outputText>
        </p:column>

Amit
  • 1
  • 2
  • What do you mean with "while sorting it is not able to sort it properly"? – Jasper de Vries May 24 '17 at 09:15
  • Hello Jasper , Thanks for helping ,Its like there is a header called "Airport Name" which is been set to sort column. and if i click it it arranges but next page it will be unsorted. and if i click again to make it desending order it doesnt do anything. so not sure if its actually sorting also. I am trying to pass the :sortField by setting it from the code not sure if that is also correct way of doing. – Amit May 24 '17 at 09:29
  • Please add your (minimal and relevant) XHTML code – Jasper de Vries May 24 '17 at 13:30
  • Hi Jasper , Added xhtml code – Amit May 24 '17 at 14:15
  • What is the scope of your bean? I would advice to use `ViewScoped`. – Jasper de Vries May 24 '17 at 14:51
  • You should call fetch (load method in DataModel) after sorting is changed – Vasil Lukach May 25 '17 at 19:50
  • Hi Vasil, I guess load method is been called automatically if i click on header sorting. I checked in debug mode also if i click header to sort it actually calls load method automatically – Amit May 26 '17 at 07:23

0 Answers0