Reputation: 2423
In a JSF/JPA web application, end user want to list several thousand bills. When using normal data tables, I use a pagination helper, which list a range of bills for each page so that only a limited number of bills are added to an ArrayList. I prefer to use Primefaces datatable with automatic pagination with several cool options. But the issue it that I have to list all the bills to an ArrayList which utilize large amount of memory. Is there any way to use pagination in PrimeFaces datatable without loading all the Bills to an ArrayList ?
Upvotes: 1
Views: 1159
Reputation: 6570
Yes, you can use a lazy load data model for that.
There's a very detailed explanation on how to do that in this article.
Basically what you need to do is to write your code for a method like this
public Collection<T> getAll(
int first,
int pageSize,
String sortField,
SortOrder sortOrder,
Map<String, String> filters)
All the pagination is done in the DB, so you'll only retrieve records that fit your page in the UI.
The article describes everything you need to use it.
One thing that is outdated in the article is how to deal with numeric values when you want to filter the dataTable by their values. You'll have to deal with the conversions yourself, for example (this goes in the "filter" section)
if (pathType.equals(Long.class)){
try{
filterCondition = cb.and(filterCondition, cb.equal(pathFilterNonString, Long.valueOf(filter.getValue())));
}catch(java.lang.NumberFormatException nfe){
//ignore
//java.lang.NumberFormatException: For input string: "a"
}
}else if (pathType.equals(Integer.class)){
try{
filterCondition = cb.and(filterCondition, cb.equal(pathFilterNonString, Integer.valueOf(filter.getValue())));
}catch(java.lang.NumberFormatException nfe){
//ignore
//java.lang.NumberFormatException: For input string: "a"
}
}else if (pathType.equals(Timestamp.class)){
try{
filterCondition = cb.and(filterCondition, cb.equal(pathFilterNonString, Timestamp.valueOf(filter.getValue())));
}catch(java.lang.IllegalArgumentException e){
//ignore
//java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
}
}
I've been using this paginator in a project for a year, with PrimeFaces 4. Haven't tested with PrimeFaces 5.
Upvotes: 1