p:dataTable selections are lost after paginating a LazyDataModel

后端 未结 5 1242
春和景丽
春和景丽 2021-01-04 00:31

My problem is that after I\'ve selected a few items on the 1st page, if I paginate to another page and come back, my initial selections are not shown. I\'ve tried to impleme

5条回答
  •  醉梦人生
    2021-01-04 01:06

    While Bruno's solution works for keeping selections across paginations, it doesn't account for retaining selections on an individual page (i.e. when never changing pages).

    This problem can be resolved more simply by using the rowSelectCheckbox and rowUnselectCheckbox ajax events, in addition to having a separate "saved" row list.

    JSF:

     
       
       
       
        ...
     
    

    Backing Bean:

        private List selectedRows;
        private List selectedRowsSaved;
    
        ...
    
        public void onSelectRow(SelectEvent event){
            MyRowClass row = (MyRowClass) event.getObject();
            selectedRowsSaved.add(row);
        }
    
        public void onUnselectRow(UnselectEvent event){
            MyRowClass row = (MyRowClass) event.getObject();
            selectedRowsSaved.remove(row);
        }
    
        public List getSelectedRows(){
            return selectedRowsSaved;
        }
    
        public void setSelectedRows(List selectedRows){
            this.selectedRows = selectedRows;
        }
    

    This way the list of saved rows is always kept up to date without needing a "page" ajax event.

提交回复
热议问题