element table 多选 翻页 选中记忆

select(selection, row) {
  let existence = false; // 选中数组中是否存在该row
  for (let i = 0; i < selection.length; i++) {
    if (selection[i].id === row.id) {
      existence = true
    }
  }
  if (existence) {
    this.checkAllRows.push(row) // 存在,意味着选中,将row添加到数组选中数组中
  } else { //  不存在,意味着未选中,循环选中数组中是否存在该row,存在就移除
    for (let i = 0; i < this.checkAllRows.length; i++) {
      if (this.checkAllRows[i].id === row.id) {
        this.checkAllRows.splice(i, 1)
      }
    }
  }
},
selectAll(selection) {
  if (selection.length > 0) { //  全选,循环添加到选中数组中
    selection.forEach((item) => {
      this.checkAllRows.push(item)
    })
  } else { //  取消全选,移除选中数组中的本页的数据。
    this.checkAllRows.splice((this.where.pageNumber * this.where.pageSize - this.where.pageSize), this.where.pageSize)
  }
},
toggleSelection() {
  //  翻页时,双重循环判断选中值,并传入page.rows的值,注意:选中数组中的值传入后不会选中行
  this.checkAllRows.forEach(item1 => {
    this.page.rows.forEach(item2 => {
      if (item1.id === item2.id) {
        this.$refs.elTable.toggleRowSelection(item2, true)    // 注意:此处如果按照下面所写,传入item1,页面是没有选中状态的
      }
    })
  })

  // const index = this.page.rows.indexOf(this.checkAllRows[0]) 判断是否存在
  // console.log('index==', index)
  //  错误写法
  // this.checkAllRows.forEach((item) => {
  //   // console.log('-----------------------this.$refs------------------------------')
  //   // console.log(this.$refs.elTable)
  //   this.$refs.elTable.toggleRowSelection(item, true);
  // })
  // console.log('---------------------toggleSelection--------------------------')
  // console.log(this.checkAllRows)
},

 

查询数据的方法中调用toggleSelection()

b5f83ded7d7c044509bcc6ce374fb75c4ec.jpg

转载于:https://my.oschina.net/u/3358860/blog/3093034

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值