【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里的选中元素

相关推荐
正在学习前端的---小方同学1 小时前
vue-easy-tree树状结构
前端·javascript·vue.js
毕业设计制作和分享2 小时前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
键盘不能没有CV键5 小时前
【图片处理】✈️HTML转图片字体异常处理
前端·javascript·html
yantuguiguziPGJ5 小时前
WPF 联合 Web 开发调试流程梳理(基于 Microsoft.Web.WebView2)
前端·microsoft·wpf
大飞记Python5 小时前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
tsumikistep6 小时前
【前端】前端运行环境的结构
前端
你的人类朋友7 小时前
【Node】认识multer库
前端·javascript·后端
Aitter7 小时前
PDF和Word文件转换为Markdown的技术实现
前端·ai编程
mapbar_front7 小时前
面试问题—上家公司的离职原因
前端·面试
昔人'8 小时前
css使用 :where() 来简化大型 CSS 选择器列表
前端·css