首页 > 解决方案 > 多次调用 LazyDataModel 初始加载

问题描述

我有这个用 Primefaces 的 LazyDataModel 实现的数据表。一切正常,除了我第一次打开页面或刷新时的初始加载。load 方法被调用了 6-7 次。

当我更改页面、过滤或排序时,它正常工作,加载只调用一次。

由于我执行对数据库的调用,因此我想防止它在需要时被调用。

我有以下内容:

XHTML:

    <p:outputPanel id="documentsPanel">
        <h:form id="documentsForm">
            <p:dataTable id="documentsTable"
                         widgetVar="documentsTable"
                         var="doc"
                         value="#{MyManagedBean.model}"
                         paginator="true" rows="20"
                         paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {CurrentPageReport}"
                         currentPageReportTemplate="{totalRecords} #{c.NumberOfRequestFound}"
                         lazy="true">
     ...

            </p:dataTable>
        </h:form>
    </p:outputPanel>

爪哇:

public class DocumentEncodingDataModel extends LazyDataModel<SelectableDocumentToBeEncodedDTO> {

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

@Override
public int getRowCount() {
    return super.getRowCount();
}

@Override
public SelectableDocumentToBeEncodedDTO getRowData(String rowKey) {
    log.debug("getRowData");
    if (!StringUtils.isBlank(rowKey)) {
        for (SelectableDocumentToBeEncodedDTO doc : currentDocumentsToBeEncoded) {
            if (doc.getDocumentToBeEncoded().getId().toString().equals(rowKey)) {
                return doc;
            }
        }
    }
    return null;
}

@Override
public Object getRowKey(SelectableDocumentToBeEncodedDTO doc) {
    return doc.getDocumentToBeEncoded().getId();
}

}

Primefaces 版本:6.2 JSF 版本:2.1

标签: primefacesprimefaces-datatable

解决方案


推荐阅读