I am using PrimeFaces 5.0. I have implemented LazyDataModel
and I am able to load the rows, however, when I paginate, the getRowData(String rowKey)
is called but rowKey
is null
. It gets the correct key on the first page but is always empty when I paginate to the next page or previous page.
<p:dataTable id="sublTableId"
value="#{submissionListBean.lazyModel}"
var="selRow"
selectionMode="single"
selection="#{submissionListBean.selectedSubmissionListRec}"
resizableColumns="true"
paginator="true"
rows="50"
paginatorPosition="both"
paginatorAlwaysVisible="true"
scrollable="true"
scrollHeight="300px;"
scrollWidth="100px;"
frozenColumns="5"
paginatorTemplate="{PreviousPageLink} {NextPageLink}"
lazy="true"
rowIndexVar="rowIndex">
<p:ajax event="rowSelect"
update=":submissionListForm:thrdListBtn"
listener="#{submissionListBean.onRowSelect}"/>
Note: I found a post which mentioned that we need to remove the rowkey
in order for the getRowKey()
to get called automatically - so I have removed that attribute:
Reference: Primefaces DataTable+LazyDataModel won't pass setPropertyActionListener in request scope
Managed Bean: [Session scoped]
public void searchByCriteria() {
String actionName = "searchByCriteria";
log.debug("SubmissionListBean:" + actionName);
try {
request = getSubmissionListSearchRequestDto();
// Pagination: Lazy Data Model
lazyModel = new LazySubmissionListDataModel(userContext, auditService, request);
displayRecList = lazyModel.getDatasource();
....
}
}
LazySubmissionListDataModel.java
public class LazySubmissionListDataModel extends LazyDataModel<AuditSubmissionListDto> {
private static final long serialVersionUID = 429415464812534969L;
private List<AuditSubmissionListDto> datasource;
private AuditSubmissionListDto request;
private IAuditService auditService;
private CmUserContext userContext;
int startPage = 1;
int totalRowCount;
public LazySubmissionListDataModel(CmUserContext userContext, IAuditService auditService,
AuditSubmissionListDto request) {
this.auditService = auditService;
this.userContext = userContext;
this.request = request;
}
@Override
public List<AuditSubmissionListDto> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
datasource = new ArrayList<AuditSubmissionListDto>();
// Calculate pages for pagination
if (first == 0) {
startPage = 1;
} else {
startPage = (first / pageSize) + 1;
}
// Call the service to get the resultlist
try {
datasource = getAuditService().getSubmissionListRecords(userContext, request, pageSize, startPage);
if ((datasource != null) && (datasource.size() > 0)) {
totalRowCount = datasource.get(0).getTotalRowsCount().intValue();
}
setRowCount(totalRowCount);
setWrappedData(datasource);
} catch (BesCmUiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return datasource;
}
@Override
public AuditSubmissionListDto getRowData(String rowKey) {
for (AuditSubmissionListDto dto : datasource) {
if (dto.getSubmissionId().equals(rowKey)) {
return dto;
}
}
return null;
}
@Override
public AuditSubmissionListDto getRowData() {
// TODO Auto-generated method stub
return super.getRowData();
}
@Override
public String getRowKey(AuditSubmissionListDto dto) {
return dto.getSubmissionId();
}
/*
* (non-Javadoc)
* @see org.primefaces.model.LazyDataModel#isRowAvailable()
*/
@Override
public boolean isRowAvailable() {
// TODO Auto-generated method stub
return super.isRowAvailable();
}
/*
* (non-Javadoc)
* @see org.primefaces.model.LazyDataModel#getRowCount()
*/
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return super.getRowCount();
}
/*
* (non-Javadoc)
* @see org.primefaces.model.LazyDataModel#getRowIndex()
*/
@Override
public int getRowIndex() {
// TODO Auto-generated method stub
return super.getRowIndex();
}
/*
* (non-Javadoc)
* @see org.primefaces.model.LazyDataModel#setRowIndex(int)
*/
@Override
public void setRowIndex(int rowIndex) {
// TODO Auto-generated method stub
super.setRowIndex(rowIndex);
}
/*
* (non-Javadoc)
* @see org.primefaces.model.LazyDataModel#setWrappedData(java.lang.Object)
*/
@Override
public void setWrappedData(Object list) {
// TODO Auto-generated method stub
super.setWrappedData(list);
}
/*
* (non-Javadoc)
* @see org.primefaces.model.LazyDataModel#getPageSize()
*/
@Override
public int getPageSize() {
// TODO Auto-generated method stub
return super.getPageSize();
}
/*
* (non-Javadoc)
* @see org.primefaces.model.LazyDataModel#getWrappedData()
*/
@Override
public Object getWrappedData() {
// TODO Auto-generated method stub
return super.getWrappedData();
}
/*
* (non-Javadoc)
* @see org.primefaces.model.LazyDataModel#setPageSize(int)
*/
@Override
public void setPageSize(int pageSize) {
// TODO Auto-generated method stub
super.setPageSize(pageSize);
}
}
rowKey="#{selRow.submissionId}"
- an attribute of<p:dataTable>
. There is no need to explicitly override thegetRowKey()
method in the associated bean. – Tiny<p:commandButton>
s in conjunction with a<f:setPropertyActionListener>
is completely unspecified. The CRUD operations using a<p:dataTable>
can be performed/handled in way a which is quite tidy. – Tinypublic void onRowSelect(SelectEvent event) {...}
in your backing bean? – Tiny