How to change setRowCount of lazyloading of p:datatable dynamically

爷,独闯天下 提交于 2019-12-20 02:45:18

问题


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.


回答1:


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.



回答2:


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}" >


来源:https://stackoverflow.com/questions/19396746/how-to-change-setrowcount-of-lazyloading-of-pdatatable-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!