I have a situation where the pagination should be dynamically. Meaning that it should change with each call of the load method.
I want set the setRowCount() method dynamically and give the pagination for the dataTable
@Override
public List<ProjectMasterModel> load(int first,int pageSize,StringsortField, SortOrder sortOrder, Map<String, String> filters) {
List<ProjectMasterModel> data=new ArrayList<ProjectMasterModel>();
LazyDataModel<ProjectMasterModel> newdata = null;
ProjectMilestoneDaoImpl milestoneDaoImpl=(ProjectMilestoneDaoImpl) ObjectFactory.getBean("projectMilestoneDao");
SessionFactory sessionFactory=(SessionFactory) ObjectFactory.getBean("sessionFactory");
sessionFactory.getCurrentSession().beginTransaction();
try{
data.addAll(milestoneDaoImpl.populateLazyRandomProjects(first,pageSize));
setRowCount(milestoneDaoImpl.getRowCount_Of_ProjectList());
// very important line to show the pagination
}catch(Exception e){
CmsLogger.errorLog(LazyProjectDataModel.class, e);
}finally{
sessionFactory.getCurrentSession().close();
}
if(sortField != null)
Collections.sort(data,new ProjectMasterModel());
return data;
}
Here I have used a query to fetch the data size to set the row count.
In a given situation their may be number of records added to the Database.
So The pagination should increment dynamically.
But if I change the setRowCount() method to a dynamic value it doesn't reflect it keeps its original value which was set for the first time.
PrimeFaces did not support this out of the box. A fix has been checked in to trunk on Feb 11th 2016, tagged 6.0 (so it should at least be in the current 6.0RCx releases). I'm not sure if it is in the Elite release >=5.2.20 or >=5.3.7 (from Feb 12th)
One important reason for this not working is that the updated rowCount you might do in the load method serverside is not applied to the paginator client side. However, since it is transferred from the server to the client, you can update it in the oncomplete of each ajax call (page, sort, filter). In fact, that is a large part of the patch (the other part is reading the value from the ajax response).
Combined calling this in e.g. the oncomplete of a ajax page event will solve the issue:
function updatePaginator(xhr, status, args) {
var paginator = PF('DataTableWidgetVar').paginator;
paginator.cfg.rowCount=args.totalRecords;
paginator.cfg.pageCount = Math.ceil(value / paginator.cfg/rows)||1;
paginator.updateUI();
}
You can then in each call in the load method,
- Try to read pageSize+1 records
- Set the count to this (pageSize+1) if you can read pageSize+1 (but still return pageSize records)
- Set the count to the number of rows read if they are pageSize or less.
MyMB.java
List<NameClass> listResult = new LazyDataModel<NameClass>() {
private static final long serialVersionUID = 1L;
@Override
public List<NameClass> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
List<NameClass> result = dao.findReg(....., pageSize, page);
setRowCount(numberOfResult);
return result;
}
};
getListResult() {..}
setListResult(List<NameClass> l ) {...}
myPage.xhtml
<p:dataTable lazy="true"
value="#{myMB.listResult}"
var="model"
paginator="true"
rows="#{myMB.pageSize}"
paginatorPosition="bottom"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" >