0
votes

I am trying to implement lazyloading for Primefaces datatable. I have a filter condition which I have implemented as

public List<Request> getRequest(int startingAt, int maxPerPage,
            String sortField, SortOrder sortOrder, Map<String, 
String> filters) {
        Criteria criteria = sessionFactory.getCurrentSession().
            createCriteria(Request.class);          
        for (Map.Entry<String, String> entry : filters.entrySet()) {            
            if(entry.getValue()!=null){             
                criteria.add(Restrictions.ilike("prNo",
                "%"+entry.getValue()+"%"));             
            }
        }
        criteria.setMaxResults(maxPerPage);
        criteria.setFirstResult(startingAt);
        return criteria.list();
    }

and in jsf page

<p:column id="prNo" filterBy="#{req.prNo}"   
                headerText="PR No">                 
                <h:outputText value="#{req.prNo}" />
            </p:column>

The issue I am facing is when filtered rows displays n datatable pagination numbers doesn't change at all, it shows as of the actual number of records. E.g. If filter matches a condition and number of rows for filter condition is 12, then pagination should display as 1 of 3 and pages should be like 1 2 3. See the attached screen shot.enter image description here

Lazymodel rowCount is set as

lazyModel.setRowCount(getRequestService().getRequestCount());

it's implementation

int count = ((Long) sessionFactory.getCurrentSession()
                .createQuery("select count(*) from Request").uniqueResult())
                .intValue();

How could I achieve this?

1
When you filtering your data , make sure that setRowCount will be set with a size of the filtered data something like lazyModel.setRowCount(criteria.list().size());Daniel
@Daniel If I am not using DataModel for LazyLoading what is the best approach to get criteria.list().size() in managedbean? And is it a good idea of not using DataModel for LazyLoading and overide load method directly in managedbean?Jacob
INMO , you better use LazyDataModel (extend it just like in the showcase) , inside your overridden load method you should do the filter logic and have the result , than get its size and use it...Daniel
@Daniel Thanks for the suggestion. I have tried to implement LazyDataModel and for some reason I am getting nullpointer exception. I have created a new post for this stackoverflow.com/questions/13967915/…Jacob
@Daniel If you post an answer I will be very glad to accept it.Jacob

1 Answers

2
votes

INMO , you better use LazyDataModel (extend it just like in the showcase - DataTable - Lazy Loading)

Inside your overridden load method you should do the filter logic and have the result , than get its size and use it...