【vue】element el-table怎么实现跨页勾选

【vue】element el-table怎么实现跨页勾选

需求

由后端提供分页接口,每一次翻页el-table的数据都会被刷新一次,这种情况下怎么实现跨页的勾选

代码

html 复制代码
<el-table
      ref="multipleTable"
      v-loading="loading"
      :data="operationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
</el-table>
javascript 复制代码
handleSelectionChange(e) {
      if(this.loading) return 
      for(let listItem of this.operationList){
        let eIndex = e.findIndex(item=>item.ic===listItem.ic)
        if(eIndex>=0){
          //该数据再当页被选中了
          let index = this.batchRedeployIds.findIndex(item=>item.id===listItem.id)
          if(index<0){
            //如果batchRedeployIds中没有该数据则添加上
            this.batchRedeployIds.push(listItem)
          }
        }else{
          //该数据在当页没被选中
          let index = this.batchRedeployIds.findIndex(item=>item.id===listItem.id)
          if(index>=0){
            //如果batchRedeployIds中有该数据则需要删除
            this.batchRedeployIds.splice(index,1)
          }
        }
      }
    },

翻页方法

javascript 复制代码
getList() {
      请求方法(paramsObj)
        .then((res) => {
          // console.log(res.rows);
          if (res.code == '200') {
            this.operationList = res.data.records;
            this.total = res.data.total;
            // this.loading = false;
            this.$nextTick(()=>{
              for(let listItem of this.operationList){
                let index = this.batchRedeployIds.findIndex(item=>item.workOrderId===listItem.workOrderId)
                if(index>=0){
                  this.$nextTick(()=>{
                    this.$refs.multipleTable.toggleRowSelection(listItem,true)
                  })
                }else{
                  // this.$refs.multipleTable.toggleRowSelection(listItem,false)
                }
              }
              this.loading=false
            })
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

分析

1.定义了一个batchRedeployIds变量,储存所有被选中的行

2.每次勾选变化时,往batchRedeployIds里添加或删除元素

3.每次分页请求后,回填batchRedeployIds里的选中元素

相关推荐
妙团团几秒前
React学习之自定义tab组合组件
javascript·学习·react.js
2601_949809596 分钟前
flutter_for_openharmony家庭相册app实战+隐私设置实现
android·javascript·flutter
2601_949593658 分钟前
React Native 鸿蒙跨平台开发:LinearGradient 渐变动画效果
javascript·react native·react.js
黄筱筱筱筱筱筱筱10 分钟前
7.适合新手小白学习Python的异常处理(Exception)
java·前端·数据库·python
qq_1777673710 分钟前
React Native鸿蒙跨平台音乐播放器涉及实时进度更新、播放控制、列表交互、状态管理等核心技术点
javascript·react native·react.js·ecmascript·交互·harmonyos
QQ179580639610 分钟前
基于springboot+vue的hive的歌曲音乐筛选推荐系统网站(源码+lw+部署文档+讲解等)
vue.js·hive·spring boot
2501_9209317014 分钟前
React Native鸿蒙跨平台实现了简单的商品图片轮播功能,为用户提供了直观的商品图片浏览体验,帮助用户全面了解商品外观
javascript·react native·react.js·ecmascript·harmonyos
Yeats_Liao16 分钟前
微调决策树:何时使用Prompt Engineering,何时选择Fine-tuning?
前端·人工智能·深度学习·算法·决策树·机器学习·prompt
晚霞的不甘17 分钟前
Flutter for OpenHarmony 实现 iOS 风格科学计算器:从 UI 到表达式求值的完整解析
前端·flutter·ui·ios·前端框架·交互
陈希瑞20 分钟前
OpenClaw Chrome扩展使用教程 - 浏览器中继控制
前端·chrome