el-table实现跨页全选

代码如下

javascript 复制代码
<template>
    <div>
        <el-table
        ref="myTable"
        :data="tableData.slice((currentPage - 1) * pagesize, currentPage * pagesize)" 
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        @select-all="selectAll" >
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          label="日期"
          width="120">
          <template slot-scope="scope">{{ scope.row.date }}</template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          width="120">
        </el-table-column>
        <el-table-column
          prop="address"
          label="地址"
          show-overflow-tooltip>
        </el-table-column>
      </el-table>
      <div style="margin-top: 30px">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" 
        :page-sizes="[5,10,30, 50, 100]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"></el-pagination>
      </div>
    </div>
   
  </template>
  
  <script>
    export default {
      data() {
        return {
        //是否全选
            isAllSelected:false,
          //选中的exId集合
          selectList:[],
          tableData: [{
            exId:1,
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            exId:2,
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            exId:3,
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            exId:4,
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            exId:5,
            date: '2016-05-08',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            exId:6,
            date: '2016-05-06',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            exId:7,
            date: '2016-05-07',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }],
          total: 0,
          currentPage: 1, //初始页
          pagesize: 5, //    每页的数据
        }
      },
  
      methods: {
        selectAll(selection) {
      this.isAllSelected=!this.isAllSelected
      if((selection.length&&this.isAllSelected) || (!selection.length&&!this.isAllSelected)){
       // 全选
           this.tableData.forEach(item=>{
               if(!this.selectList.some(items =>  items == item.exId)){
                   this.selectList.push(item.exId)
               }
           })
       }else{
           // 全不选
           this.tableData.forEach(item=>{
               let index = -1
               this.selectList.some((items,i)=>{
                   if(items == item.exId){
                       index = i
                   }
               })
               if(index > -1) this.selectList.splice(index,1)
           })
           //清除选中的状态
           this.$refs.myTable.clearSelection()
       }
   
     },
   handleSizeChange: function (size) {
      this.pagesize = size;
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      //  this.$nextTick这段代码可以写在后端获取列表之后,用来解决切换页面,checkbox没有被选中的问题,如果是全选状态,切换页面checkbox设置为选中状态
           this.$nextTick(()=>{
               this.tableData.forEach(item=>{
                   if((!this.selectList.includes(item.exId)&&this.isAllSelected) || (!this.isAllSelected && this.selectList.includes(item.exId))){
                       this.$refs.myTable.toggleRowSelection(item,true)
                   }else{
                       this.$refs.myTable.toggleRowSelection(item,false)
                   }
               })
           })
    },
    handleSelectionChange(val) {
            this.selectList = val.map((item) => item.exId);
        }
      }
    }
  </script>
相关推荐
Patrick_Wilson1 小时前
router.replace 之后紧跟 reload,页面为什么无限刷新?
javascript·react.js·浏览器
mONESY2 小时前
JavaScript 栈、队列、数组与链表核心知识点总结
javascript·面试
ZengLiangYi3 小时前
TypeScript 项目配置:tsconfig、ESM、路径别名
javascript·typescript·aigc
晓13133 小时前
【Cocos Creator 3.x】篇——第二章 入门
前端·javascript·游戏引擎
想要成为糕糕手3 小时前
前端必修课:JavaScript 数组与数据结构底层逻辑全解析
javascript·数据结构·面试
xiaofeichaichai3 小时前
React Hooks
前端·javascript·react.js
数据知道3 小时前
C++ 层拦截:修改 Blink 引擎与 V8 绑定的底层逻辑
javascript·数据采集·指纹浏览器·风控
拉拉肥_King4 小时前
Vue 3 主题切换深度解析:从炫酷动画到零闪烁方案
前端·vue.js
2301_773643624 小时前
ceph镜像
前端·javascript·ceph
To_OC4 小时前
万字解析《JS语言精粹》之第四章:函数15大核心精髓(JS灵魂核心)
前端·javascript·代码规范