elementui el-table表格实现跨页(翻页)保存勾选状态(后端分页)

思路:后端分页导致每一页之间的勾选状态都是独立的,我们需要新建一个数组来保存这些被勾选数据的id,当切换到某个页码时,从数组中进行比对,如果当前页码有待勾选的id,则用代码让当前页码的数据勾选起来。
注意:
不要使用row-key 和 reserve-selection 。否则导致自己管理的勾选状态与el-table自身保存的勾选状态互相冲突,导致状态难以管理。我们的目的是,表格的勾选状态均由代码来控制
提示:
本文代码仅包含关键代码,用于提供思路,无法直接运行使用。

再次提醒:不要使用row-key 和 reserve-selection


第一步: 给el-table添加一些勾选的监听事件,row-click用于监听鼠标选中不同行时的勾选事件(不需要可去掉,我的需求是鼠标选中某行也要勾选);selection-change用于监听鼠标点击某行前面的勾选框、选中某行的实现的勾选、表格左上角的全选,三种状态的事件;select用于监听鼠标点击表格某行数据前方的勾选框;select-all用于监听鼠标点击表格左上角的全选框。

提示:↑此段话看不懂可跳过,等阅读完下面代码有疑问再来这段话里寻找答案。

                <el-table
                  ref="singleTable"
                  v-loading="tableLoading"
                  :data="tableData"
                  border
                  :height="tableHeight"
                  highlight-current-row
                  @row-click="rowClick"
                  @selection-change="selectionChange"
                  @select="onTableSelect"
                  @select-all="selectSingleTableAll"
                >
                  <el-table-column type="selection" width="55px" align="center" />
                  <el-table-column label="序号" type="index" align="center" :index="(index) => { return (index+1) + (currentPage-1)*pageSize }" />
                  <el-table-column prop="policyNo" label="号码" :width="flexColumnWidth('policyNo', tableData, 200, 80)" align="center" />
                </el-table>
                <el-pagination
                  :current-page="currentPage"
                  :page-sizes="[10, 20, 30, 40, 50]"
                  :page-size="pageSize"
                  :total="total"
                  layout="total, sizes, prev, pager, next, jumper, slot"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                >
                  <span style="padding-left:15px;color: #606266;font-weight: 400;">已选中<span style="padding: 0 5px;display: inline;">{{ tableAllSelectedId.length }}</span>条</span>
                  <el-button class="refresh-button" icon="el-icon-refresh" title="刷新" @click="refresh" />
                </el-pagination>
  data() {
    return {
      tableAllSelectedId: [], // 保存表格勾选的全部id
      tableAllSelectedRow: [], // 保存表格勾选的行数据
      tableData: [], // 当前所在页码的表格数据
      tableData_all: [], // 表格的全部数据
      currentPage: 1,
      pageSize: 10,
      tableLoading: false,
  computed: {
    tableData: {
      get() {
        return this.tableData // 多此一举是为了让tableData更响应式,目的在于显示勾选个数时更实时
      },
      set(val) { }
    },
  methods: {
    // 查找对象在对象数组中的位置
    findIndexInObejctArr: function(arr, obj) {
      for (let i = 0, iLen = arr.length; i < iLen; i++) {
        if (arr[i].deliverId === obj.deliverId) {
          return i
        }
      }
      return -1
    },
    // 点击表格勾选触发的事件
    onTableSelect: function(rows, row) {
      // 判断是点击了表格勾选还是取消勾选
      // true就是选中,0或者false是取消选中
      const selected = rows.length && rows.indexOf(row) !== -1
      if (!selected) {
      // 如果点击取消勾选
        const index = this.tableAllSelectedId.indexOf(row.deliverId)
        this.tableAllSelectedId.splice(index, 1) // 取消勾选,则删除id
        this.tableAllSelectedRow.splice(index, 1) // 取消勾选,则删除数据
      }
    },
    // 表格全选触发的事件
    selectSingleTableAll: function(selection) {
      // 获取当前页码所显示的数据
      const a = this.tableData
      // 获取当前页勾选的数据
      const b = selection
      let flag_inCurrentPage
      selection.forEach((item) => {
        if (item.deliverId === a[0].deliverId) {
          flag_inCurrentPage = true
          return
        }
      })
      // 后端分页,点击全选框时,当前页的勾选数目等于当前页数据个数,判断为全选
      const flag = a.length === b.length && flag_inCurrentPage
      if (flag === true) { // 切换成了全选状态
        this.tableData_all.forEach((item) => {
          if (this.tableAllSelectedId.indexOf(item.deliverId) === -1) {
            this.tableAllSelectedId.push(item.deliverId) // 如果点击全选就保存全部的id
            this.tableAllSelectedRow.push(item) // 则保存表格全部的数据
          }
        })
        // console.log('切换成了全选状态', this.tableData_all)
      } else { // 切换成了非全选状态
        this.tableAllSelectedId = [] // 如果取消全选,则清空保存的id
        this.tableAllSelectedRow = [] // // 如果取消全选,则清空保存的数据
        // console.log('切换成了非全选状态')
      }
    },
    // 获取表格数据
    getTableData: function(type) {
      this.tableLoading= true
      const params = {
        cusCode: this.activeCustomer,
        page: this.currentPage,
        rows: this.pageSize,
        address: this.queryForm.address
      }
      API.pageTableAPI(params).then(response => {
        const res = response.data.data
        if (response.data.flag === 'success') {
          this.tableData = res.content // 该后端分页的数据用于显示(渲染)使用。(因数据量过大,于是使用后端分页)
          this.total = res.total
          // 当切换页码时
          if (type === 'ishandleCurrentChange') {
            this.$nextTick(() => {
              this.tableData.forEach((item) => {
                if (this.tableAllSelectedId.indexOf(item.deliverId) > -1) {
                  this.$refs.singleTable.toggleRowSelection(item, true)
                } else {
                  this.$refs.singleTable.toggleRowSelection(item, false)
                  // console.log('去除勾选:', item)
                }
              })
            })
          }
        } else {
          this.$errorMsg(res)
        }
        this.tableLoading= false
      }).catch(error => {
        this.$errorMsg('' + error)
        this.tableLoading= false
      })
      // 请求表格的全部数据
      const params_all = {
        cusCode: this.activeCustomer,
        address: this.queryForm.address
      }
      API.pageTableAPI(params_all).then(response => {
        const res = response.data.data
        if (response.data.flag === 'success') {
          this.tableData_all = res.content // 保存一份表格的全部数据,用于跨页全选时判断使用
        } else {
          this.$errorMsg(res)
        }
        this.tableLoading= false
      }).catch(error => {
        this.$errorMsg('' + error)
        this.tableLoading= false
      })
    },
   query: function() {
      this.currentPage = 1
      this.tableAllSelectedId = [] // 点击查询按钮后,保存的勾选的id要清空
      this.tableAllSelectedRow = [] // 点击查询按钮后,保存的勾选的数据要清空
      this.getCenterList()
    },
    // 表格行点击事件
    rowClick: function(row) {
      // 只有同时高亮并选中的情况下才能取消选中
      if (this.findIndexInObejctArr(JSON.parse(JSON.stringify(this.tableAllSelectedRow)), row) > -1) {
        if (this.tableRadio === row) {
          this.tableRadio = []
          this.$refs.singleTable.setCurrentRow(null)
          this.$refs.singleTable.toggleRowSelection(row, false)
          const index = this.tableAllSelectedId.indexOf(row.deliverId)
          this.tableAllSelectedId.splice(index, 1) // 取消勾选,则删除id
          this.tableAllSelectedRow.splice(index, 1) // 取消勾选,则删除数据
        } else {
          this.tableRadio = row
          this.$refs.singleTable.setCurrentRow(row)
        }
      } else {
        this.tableRadio = row
        this.$refs.singleTable.setCurrentRow(row)
        this.$refs.singleTable.toggleRowSelection(row, true)
      }
    },
    // 多选事件
    selectionChange: function(val) {
      // 将获取到的id存入tableAllSelectedId数组(点击某行前面的勾选、选中某行的勾选、全选。三种状态都能触发此功能)
      val.forEach((item) => {
        if (this.tableAllSelectedId.indexOf(item.deliverId) === -1) {
          this.tableAllSelectedId.push(item.deliverId)
          this.tableAllSelectedRow.push(item)
        }
      })
      // console.log('selectionChange的事件:', val)
    },
    // 切换页码事件
    handleCurrentChange: function(val) {
      this.currentPage = val
      this.getTableData('ishandleCurrentChange')
    },
  • 9
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值