element ui 中table翻頁記憶上頁勾選

在使用table時很多時候需要分頁,每次從后臺請求一定條數(shù)的數(shù)據(jù),但是,有的業(yè)務(wù)中需要用到勾選。是不是會遇到勾選過第一頁以后,翻頁到第二頁的時候,第一頁的勾選被取消了呢。下面代碼可實現(xiàn)記憶勾選的功能(默認勾選的暫時有bug)

        this.change(this.mobileDatas)    //這個方法是在點擊分頁請求數(shù)據(jù)的時候調(diào)用,this.mobileDatas 這個是點擊分頁請求到的數(shù)據(jù)
      //change是可以實現(xiàn)翻頁后再去上一頁,上一頁的會有已經(jīng)有的勾選狀態(tài)
           change(data){
                for(let i = 0;i<data.length;i++){
                    for(let x = 0;x<this.multipleSelectionAll.length;x++){     
                    if(data[i].id == this.multipleSelectionAll[x].id ){
                        var c = i
                        var f = function(a){
                            setTimeout(() => {
                                vm.$refs.multipleTable.toggleRowSelection(data[a],true);
                            }, 1*a);
                        }
                        f(c)
                    }
                }
                }
                
            },
  在table的@selection-change="handleSelectionChange"中
             handleSelectionChange(val) {
                this.multipleSelection = val
                this.changePageCoreRecordData (this.multipleSelection)
           },
           changePageCoreRecordData (x) {
                // 總選擇里面的key集合
                let selectAllIds = [];
                this.multipleSelectionAll.forEach((row,index)=>{
                    selectAllIds.push(row.id);
                })
                let selectIds = []
                // 獲取當前頁選中的id
                x.forEach((row,index)=>{
                    selectIds.push(row.id);
                    // 如果總選擇里面不包含當前頁選中的數(shù)據(jù),那么就加入到總選擇集合里
                    if (selectAllIds.indexOf(row.id) < 0) {
                        this.multipleSelectionAll.push(row);
                    }else{
                        for(let i = 0; i< x.length; i ++) {
                            if (this.multipleSelectionAll[i].id == row.id) {
                                // 如果總選擇中有未被選中的,那么就刪除這條
                                this.multipleSelectionAll.splice(i, 1,x[i]);
                                break;
                            }
                        }
                    }
                })
                let noSelectIds = [];
                // 得到當前頁沒有選中的id
                x.forEach(row=>{
                    if (selectIds.indexOf(row.id) < 0) {
                        noSelectIds.push(row.id);
                    }
                })
                noSelectIds.forEach(id=>{
                    if (selectAllIds.indexOf(id) >= 0) {
                        for(let i = 0; i< this.multipleSelectionAll.length; i ++) {
                            if (this.multipleSelectionAll[i].id == id) {
                                // 如果總選擇中有未被選中的,那么就刪除這條
                                this.multipleSelectionAll.splice(i, 1);
                                break;
                            }
                        }
                    }
                })
                console.log(this.multipleSelectionAll)
            },

分頁的方法里要執(zhí)行以下上面方法,可以實現(xiàn)翻頁記憶

  handleCurrentChange(val) {
                this.page = val
                this. handleChange()
                this.changePageCoreRecordData (this.multipleSelection)
            },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容