1
votes

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);
    }
}
1
You have overridden those all methods in the bean without an absolute need that just unnecessarily makes your code messy. Just get rid of them unless overriding of those methods is absolutely necessary. In this case, you can just do rowKey="#{selRow.submissionId}" - an attribute of <p:dataTable>. There is no need to explicitly override the getRowKey() method in the associated bean.Tiny
The post you linked is quite messy as well. The intention to use <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.Tiny
Hi Thank you very much for the reply. I added the rowKey="#{selRow.submissionId}" and removed all the overridden methods from LazySubmissionListDataModel.java and retained only getters and setters, still the selected row is null onRowSelect.srmthy4
Managed Bean: public void onRowSelect(SelectEvent event) { String actionName = ON_ROW_SELECT; log.debug("SubmissionListBean:" + actionName); try { AuditSubmissionListDto selectedObj = (AuditSubmissionListDto)event.getObject();srmthy4
I do not find that listener method in your backing bean. Do you precisely have a method like public void onRowSelect(SelectEvent event) {...} in your backing bean?Tiny

1 Answers

0
votes

Finally I was able to figure out the issue, it is the frozenColumn attribute in the .xhtml with sets the rowkey as "". I was able to replicate it with the standard car's example given in Primefaces showcase as well. If anyone is aware if there is a resolution to this, appreciate if you can kindly post. Thanks!

.xhtml

<p:dataTable var="car" value="#{tableBean.lazyModel}" paginator="true" rows="10" lazy="true"  
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} 
{CurrentPageReport}{NextPageLink} {LastPageLink}"  
rowsPerPageTemplate="5,10,15"  
selection="#{tableBean.selectedCar}" selectionMode="single" 
scrollable="true" frozenColumns="1">
 <p:ajax event="rowSelect"  listener="#{tableBean.onRowSelect}" />