Praveen V
Praveen V

Reputation: 267

Pagination not working for a Lazy Loaded Data Table on First Loading

I am using JPA named queries for Loading a Lazy Loaded DataTable. and setting first and Max results as shown below.

Query query = entityManager.createNamedQuery("StudyplanCategory.findByStatusAndLimit");
int end=(start*pageNumber);
query.setParameter("status", status);
query.setParameter("start", start);
query.setParameter("end", end);
query.setMaxResults(end - start);

The load method is given below:

public List<StudyplanCategory> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {  
                List<StudyplanCategory> data = new ArrayList<StudyplanCategory>();
                //System.out.println("Page First Value:"+first+"PageSize Value:"+pageSize);
                datasource=categoryService.findDynaEditStudyPlan("NOT_USER_SPECIFIC",first,pageSize);
                //filter  
                for(StudyplanCategory studyplanCategory : datasource) {  
                    boolean match = true;  
                    for(Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {  
                        try {  
                            String filterProperty = it.next();  
                            String filterValue = filters.get(filterProperty).toLowerCase();  
                            String fieldValue = String.valueOf(studyplanCategory.getClass().getDeclaredField(filterProperty).get(studyplanCategory)).toLowerCase();  
                            //System.out.println("fieldValue............."+fieldValue);
                            if(filterValue == null || fieldValue.startsWith(filterValue)) {  
                                match = true;  
                            }  
                            else {  
                                match = false;  
                                break;  
                            }  
                        } catch(Exception e) {  
                            match = false;  
                            System.out.println("The Exception occured at"+e);
                        }   
                    }  

                    if(match) {  
                        data.add(studyplanCategory);  
                    }  
                }  

                //sort  
                if(sortField != null) {  
                    Collections.sort(data, new LazySorter(sortField, sortOrder));  
                }  

                //rowCount  
                int dataSize = data.size();  
                this.setRowCount(dataSize);  

                //paginate  
                if(dataSize > pageSize) {  
                    try {  
                        return data.subList(first, first + pageSize);  
                    }  
                    catch(IndexOutOfBoundsException e) {  
                        return data.subList(first, first + (dataSize % pageSize));  
                    }  
                }  
                else {  
                    return data;  
                }  
            }  

But when the table is loaded Next Buttons are not active because I am loading only those data required to load the first page. How can I Solve this.

Upvotes: 0

Views: 1828

Answers (1)

BalusC
BalusC

Reputation: 1108632

You need to fire another query which sets the total rowcount. Basically, in LazyDataModel#load():

public List<StudyplanCategory> load(...) {
    setRowCount(studyplanCategoryService.count());
    return studyplanCategoryService.list(...);
}

Unrelated to the concrete problem, you should actually be using Query#setFirstResult() to set the first record index.

Upvotes: 1

Related Questions