0

Using LazyDataModel does not display columns in the datatable.

I have implemented custom LazyDataModel and I have implemented load method init. However, when I pass the datamodel as a value to the datatable, the columns are not rendered/displayed. But when I pass it as a list, the columns are rendered/displayed.

My LazyDataModel class

public class SearchPmrLazyDataModel extends LazyBaseDataModel<SearchDTO> {
 public SearchPmrLazyDataModel() {
    }

    public void setData() {
        if (searchCriteria == null) {
            searchCriteria = new PmrSearchCriteriaDTO();
        }
       setWrappedData(pmrService.searchPmr(searchCriteria, teamId));
    }

    @Override
    public List<PmrSearchResultViewDTO> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {

        setData();
        searchCriteria.setPageRequest(determinePage(first, pageSize));
        List<PmrSearchResultViewDTO> data = this.cachedData;

                // rowCount
        int dataSize = data.size();
        this.setRowCount(dataSize);

        // paginate

        if (dataSize > pageSize) {
            try {
                return data.subList(first, first + pageSize);
            } catch (IndexOutOfBoundsException e) {
                return data.subList(first, first + (dataSize % pageSize));
            }
        } else {
            return data;
        }
    }

    @Override
    public void setRowIndex(int rowIndex) {
        if (rowIndex == -1 || getPageSize() == 0) {
            super.setRowIndex(-1);
        }
        else
            super.setRowIndex(rowIndex % getPageSize());
    }

    @Override
    public Object getRowKey(PmrSearchResultViewDTO obj) {
        return obj.getId();
    }
} 

My bean

public LazyDataModel<SearchDTO> getCorresLazyDataModel(String selectedTabName, String selectedStateName, String selectedStateName2) {
       //returns corresponding lazy data model according to a business logic
        return getLazyDataModel(selectedTabName, selectedStateName, selectedStateName2);
    }

My XHTML page

<p:dataTable
    value="#{formBean.getCorresLazyDataModel(pmrStatusTab, pmrState, pmrState2)}"
    var="pmr" id="pmrStatusResultTable#{pmrStatusTab}#{pmrState}"
    widgetVar="pmrStatusResultTable#{pmrStatusTab}#{pmrState}"
    style="overflow: auto; width: auto! important; table-width:fixed"
    rows="10" rowKey="#{pmr.id}" rowStyleClass="odd, even"
    reflow="true" scrollWidth="#{formBean.scrollWidth}"
    scrollable="true" scrollRows="10" paginator="true" lazy="true"
    selectionMode="single" selection="#{formBean.selectedPmr}">
    <p:ajax event="rowSelect" process="@this"
        listener="#{formBean.onRowSelect}" />
    <p:columns value="#{formBean.columns}" var="column"
        columnIndexVar="colIndex" sortBy="#{pmr[column.property]}"
        sortable="true">
        <f:facet name="header">
            <h:outputText value="#{column.header}" />
        </f:facet>
        <h:outputText value="#{pmr[column.property]}" />
    </p:columns>
</p:dataTable>

The columns are not rendered/displayed only if I pass datamodel directly. If I pass the value as list, they are displayed, is there anything which I am missing?

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
user1734698
  • 157
  • 2
  • 2
  • 17
  • What did you debug? And always try to make a [mcve]. It helps you narrow down the problem before asking a question. Could be many (for us invisible) things going on. Also read http://www.stackoverflow.com/tags/jsf/info about database in a [mcve] – Kukeltje May 27 '19 at 09:24
  • 1
    It does not look like you followed the showcase example: https://www.primefaces.org/showcase/ui/data/datatable/lazy.xhtml – Melloware May 27 '19 at 11:59
  • 1
    For a given set of parameters, does `getLazyDataModel(selectedTabName, selectedStateName, selectedStateName2);` return the same instance of LazyDataModel within a request (or even view) context for multiple invocations? – Selaron May 27 '19 at 12:02
  • Yes... All returns instances of same LazyDataModel – user1734698 May 29 '19 at 11:50

0 Answers0