如何在分页后保留选择列表和复选框,同时延迟加载素面数据 table?

How to retain selection list and check boxes after pagination while lazy loading in prime faces data table?

我在素面数据 table 上实现了延迟加载。它工作正常,但是当页面更改时,选择被清除。如何在更改页面并 return 返回页面时保留其他页面的选择。 I found one related question here.

This answer 帮助我保留了选中的对象,但在返回页面时无法保留 table 处的复选框。

使用 Prime Faces 版本 6.2

由于 OP 不想升级到 6.2 以上,解决方案是此处列出的解决方法: https://forum.primefaces.org/viewtopic.php?f=3&t=47224

添加一个 AJAX toggleSelect 事件

<p:ajax event="toggleSelect" 
global="false" 
immediate="true"  
update=":usersManagerForm:usersTable:usersActions" 
listener="#{usersManagerBean.onToggleSelect}" />

和支持 bean 方法:

public void onToggleSelect(ToggleSelectEvent event) {
        List<Compte> comptesListOfCurrentPage = (List<Compte>) compteLazyDataModel.getWrappedData();
        if(event.isSelected()) {
            selectedComptesList.addAll(comptesListOfCurrentPage);
        } else {
            selectedComptesList.removeAll(comptesListOfCurrentPage);
        }
    }

我在 2 个答案的帮助下得到了解决方案

  1. From this

.xhtml

<p:dataTable selection="#{myBean.selectedRows}" ... >
   <p:ajax event="rowSelectCheckbox" process="@this" listener="#{myBean.onSelectRow}" />
   <p:ajax event="rowUnselectCheckbox" process="@this" listener="#{myBean.onUnselectRow}" />
<p:ajax event="page" process="@this" listener="#{myBean.onPagination}" />
<p:ajax event="toggleSelect" process="@this" listener="#{myBean.onToggle}" />
   <p:column selectionMode="multiple" ... />
    ...
 </p:dataTable>

.java

    private List<MyRowClass> selectedRows;
    private List<MyRowClass> selectedRowsSaved;

    ...

    public void onSelectRow(SelectEvent event){
        selectedRowsSaved.add((MyRowClass) event.getObject());
    }

    public void onUnselectRow(UnselectEvent event){
        selectedRowsSaved.remove((MyRowClass) event.getObject());
    }
    
    public void onToggle(ToggleSelectEvent event){
    List<Object> completeListOfCurrentPage = (List<Object>) compteLazyDataModel.getWrappedData();
        if(event.isSelected()) {
            selectedRowsSaved.addAll(comptesListOfCurrentPage);
        } else {
            selectedRowsSaved.removeAll(comptesListOfCurrentPage);
        }
    }

    public void onPagination(){
        if(selectedRowsSaved!=null && selectedRowsSaved.isEmpty()}{
            selectedRows.addAll(selectedRowsSaved);
        }
    }
    public List<MyRowClass> getSelectedRows(){
        return selectedRowsSaved;
    }

    public void setSelectedRows(List<MyRowClass> selectedRows){
        this.selectedRows = selectedRows;
    }