question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataTable: Lazy getRowData Repeat the query twice

See original GitHub issue

1) Environment

PrimeFaces version: 8.0

org.joinfaces.primefaces-spring-boot-starter 4.3.0

2) Expected behavior

I hope that when I select a piece of data in the datatable, getRowData is executed only once.

3) Actual behavior

When I select a piece of data in the datatable, getRowData is executed twice, which is very strange.

AbstractLazyModel

    public abstract class AbstractLazyModel<T> {

    private T[] selects;
    private T select;

    private LazyDataModel<T> lazyDataModel;

    protected abstract LambdaQueryWrapper<T> getQueryParam();

    protected abstract BasicService getBasicService();

    protected  abstract String getKey(T t);


    public LazyDataModel<T> getResultModel() {
        if (lazyDataModel == null) {
            lazyDataModel = new LazyDataModel<T>() {
                @Override
                public List<T> load(int first, int pageSize, Map<String, SortMeta> sortMeta, Map<String, FilterMeta> filterMeta) {
                    int currentPage = (first / pageSize) + 1;
                    Page<T> page =  new Page(currentPage,pageSize);
                    page.setOptimizeCountSql(false);
                    return getBasicService().getResultList(page,getQueryParam());
                }

                @Override
                public T getRowData(String rowKey) {
                    return   (T)getBasicService().findById(rowKey);
                }

                @Override
                public String getRowKey(T t) {
                    return getKey(t);
                }
            };
            lazyDataModel.setRowCount( getBasicService().getRowCount(getQueryParam()));
        }
        return lazyDataModel;
    }

    protected void refresh() {
        this.setSelect(null);
        search();
    }

    public void search() {
        lazyDataModel = null;
    }

Example Bean

@Slf4j
@Data
@Component
@ViewScoped
public class ErpSysCodeTypeView extends AbstractLazyModel<ErpSysCodeType> implements java.io.Serializable {

    @Autowired
    private ErpSysCodeTypeService erpSysCodeTypeService;


    @Autowired
    private PrimeFaces primeFaces;

    @Autowired
    private FacesContext facesContext;

    private SysCodeTypeRequest sysCodeTypeRequest;

    private String codeName;


    private List<ErpSysCodeType> sysCodeTypeList;

    private ErpSysCodeType oldErpSysCodeType;


    public void searchListener() {
        this.refresh();
    }

    public void onRowSelect(SelectEvent<ErpSysCodeType> event) {
      // this.setSelect( event.getObject());
    }




    @PreDestroy
    public void destroy() {
        log.info("ErpSysCodeTypeView destroy");
    }


    @Override
    protected LambdaQueryWrapper<ErpSysCodeType> getQueryParam() {
        return new LambdaQueryWrapper<ErpSysCodeType>().
                likeRight(!StringUtils.isEmpty(codeName),ErpSysCodeType::getName,codeName);
    }


    @Override
    protected BasicService getBasicService() {
        return erpSysCodeTypeService;
    }

    @Override
    protected String getKey(ErpSysCodeType erpSysCodeType) {
        return String.valueOf(erpSysCodeType.getId());
    }

}

Example Xhtml

  <p:dataTable id="dataTableId"
                                         widgetVar="sysCodeTypeTableVar"
                                         value="#{erpSysCodeTypeView.resultModel}"
                                         paginatorPosition="bottom"
                                         rowIndexVar="rowIdx"
                                         rows="10"
                                         lazy="true"
                                         selection="#{erpSysCodeTypeView.select}"
                                         selectionMode="single"
                                         rowKey="#{_sysCodeType.id}"
                                         var="_sysCodeType"
                                         emptyMessage="暂无数据."
                                         paginator="true"
                                         currentPageReportTemplate="查询到{totalRecords}条记录,本页显示{startRecord}~{endRecord};第{currentPage}/{totalPages}页"
                                         paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                         rowsPerPageTemplate="5,10,15">

                                <p:ajax event="rowSelect"  listener="#{erpSysCodeTypeView.onRowSelect}" process="@this"/>

                                <p:column width="50">
                                    <f:facet name="header">序号</f:facet>
                                    <h:outputText value="#{rowIdx + 1}"/>
                                </p:column>

                                <p:column headerText="字典代码">
                                    <h:outputText value="#{_sysCodeType.code}"/>
                                </p:column>

                                <p:column headerText="字典名称">
                                    <h:outputText value="#{_sysCodeType.name}"/>
                                </p:column>

                                <p:column headerText="启用">
                                    <h:outputText value="#{_sysCodeType.status == 1 ? '启用':'关闭'}"/>
                                </p:column>

                                <p:column headerText="创建时间">
                                    <h:outputText value="#{_sysCodeType.createDate}">
                                        <f:convertDateTime pattern="yyyy-MM-dd HH::mm:ss"/>
                                    </h:outputText>
                                </p:column>

                            </p:dataTable>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:29 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-5512commented, Aug 2, 2020

@eric-5512 Couldn’t you do something like this?

if (lazyDataModel == null) {
    lazyDataModel = new LazyDataModel < T > () {
        private T lastSelected = null;
        private String lastRowKey = null;

        @Override
        public List < T > load(int first, int pageSize, Map < String, SortMeta > sortMeta, Map < String, FilterMeta > filterMeta) {
            int currentPage = (first / pageSize) + 1;
            Page < T > page = new Page(currentPage, pageSize);
            page.setOptimizeCountSql(false);
            return getBasicService().getResultList(page, getQueryParam());
        }

        @Override
        public T getRowData(String rowKey) {
            if (lastRowKey.equals(rowKey)) {
                return lastSelected;
            } else {
                lastRowKey = rowKey;
                lastSelected = (T) getBasicService().findById(rowKey);
                return lastSelected;
            }
        }

        @Override
        public String getRowKey(T t) {
            return getKey(t);
        }
    };
    lazyDataModel.setRowCount(getBasicService().getRowCount(getQueryParam()));
}

@melloware No problem, it’s all right. But it’s strange. Thank you.

0reactions
Rapstercommented, Nov 28, 2020

I might have a solution for this

Read more comments on GitHub >

github_iconTop Results From Across the Web

PrimeFaces DataTable lazy loading is loaded twice
The problem is that overridden method load() in my LazyDataModel implementation is being called twice every time I try to filter the table....
Read more >
Datatable. Lazy + livescroll + filter = duplicate items
I've datatable with lazy loading and live scroll attributes. ... (lazy loaded the last one of the 31 list), the first 15 items...
Read more >
Datatables table loads twice, first time without table data
Hello! I'm having one strange thing with datatables table: it loads twice, first time for a very short period of time (less than...
Read more >
jquery datatable getting row data Code Example
var table = $('#example').DataTable(); $('#example tbody').on( 'click', 'tr', function () { console.log( table.row( this ).data() ); } );
Read more >
primefaces - issue #2487 - Google Code
Problem with dataTable rowKey after removing a few table rows ... getRowData(DataTable.java:878) at org.primefaces.component.datatable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found