I am trying to achieve pagination for List data with PagedListHolder
from Spring but when set input size greater than 15 ,its now returning expected output.
private List<WorklistResponse> getPaginatedResponseResult(List<WorklistResponse> worklistResponse, FilterRequest filterRequest) {
PagedListHolder<WorklistResponse> pagedListHolder = new PagedListHolder<WorklistResponse>();
pagedListHolder.setSource(worklistResponse);
pagedListHolder.setPage(filterRequest.getCurrentPage());// 1
pagedListHolder.setPageSize(filterRequest.getPageSize());// 29
System.out.println(worklistResponse.size());//total input list size 30
System.out.println(pagedListHolder.getPageSize()); // after setting size 29
System.out.println(pagedListHolder.getPageList().size());// actual page element size result is 1 which is not correct ,it should be 29
return pagedListHolder.getPageList();
}
In class PagedListHolder
, below methods is impacting on result:
public List<E> getPageList() {
return this.getSource().subList(this.getFirstElementOnPage(), this.getLastElementOnPage() 1);
}
How I can get correct page size for any input?
CodePudding user response:
You have 30 records, page 1 and page size 29 returns 1 record because: PagedListHolder
is a simple state holder for handling lists of objects, separating them into pages. Page numbering starts with 0.
If structurally your first page is 1, then subtract 1 when submitting the page number. e.g:
pagedListHolder.setPage(filterRequest.getCurrentPage() - 1);