1

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);
    }
}
Community
  • 1
  • 1
srmthy4
  • 81
  • 1
  • 2
  • 7
  • 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 ``. There is no need to explicitly override the `getRowKey()` method in the associated bean. – Tiny Dec 31 '14 at 19:58
  • The post you linked is quite messy as well. The intention to use ``s in conjunction with a `` is completely unspecified. The CRUD operations using a `` can be performed/handled in way a which is quite tidy. – Tiny Dec 31 '14 at 20:21
  • 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 Dec 31 '14 at 20:50
  • Managed Bean: public void onRowSelect(SelectEvent event) { String actionName = ON_ROW_SELECT; log.debug("SubmissionListBean:" + actionName); try { AuditSubmissionListDto selectedObj = (AuditSubmissionListDto)event.getObject(); – srmthy4 Dec 31 '14 at 20:57
  • 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 Dec 31 '14 at 20:58
  • Uh! we were typing at the same time. Do you mean to say that `selectedObj` returns `null`? No obvious symptoms can be found further. Please try looking into the showcase [examples](http://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml), in case you might miss something obvious. – Tiny Dec 31 '14 at 21:06
  • Yes, selectedObj is null when I paginate to the next page. I will look again in the examples. Thanks for the feedback., please let me know if you think of any other pointers. Have a great year. – srmthy4 Dec 31 '14 at 21:51
  • Here `selection="#{submissionListBean.selectedSubmissionListRec}"`, Does the bean name `submissionListBean` correspond to `LazySubmissionListDataModel` (either by `@ManagedBean` or `@Named`). Is `selectedSubmissionListRec` of type `AuditSubmissionListDto`? Here `value="#{submissionListBean.lazyModel}"`, you should just do `value="#{submissionListBean}"` because you have already overridden the `load()` method. The lazy model should not be explicit. – Tiny Dec 31 '14 at 22:13
  • I have a Session scoped Managed Bean: SubmissionListBean.java. Yes selectedSubmissionListRec is of type AuditSubmissionListDto. I instantiate the LazyDataModel - LazySubmissionListDataModel in the searchByCriteria() method of the SubmissionListBean as our requirement is show empty table on load and then search for the data. ~Thanks – srmthy4 Dec 31 '14 at 22:31

1 Answers1

0

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}" />
srmthy4
  • 81
  • 1
  • 2
  • 7
  • Please file an issue in the PF issuelist – Kukeltje Jan 29 '15 at 00:16
  • @Kukeltje, Yes, I have filed and issue in PF issues list: Issue 7850:https://code.google.com/p/primefaces/issues/detail?id=7850&q=lazydatamodel&colspec=ID%20Type%20Status%20Priority%20TargetVersion%20Reporter%20Owner%20Summary – srmthy4 Jan 30 '15 at 01:26