el-table 选中/全选/反选/分页选中存储方法

🌰 html:

<el-table
  :data="table"
  ref="multipleTable"
  @select-all="selectAllChange"
  @select="selectChange"
  @row-click="handleCurrentChange"
>
  <el-table-column type="selection"></el-table-column>
  <el-table-column label="状态" align="left"></el-table-column>
</el-table>

🌰 js:

data(){
	return {
		table:[],
		selectedlist:[],//点击选中数据
	}
},
methods:{
	//checkbox点击时
	selectChange(selection, row) {
	  this.changeSelectData(row);
	},
	
	//每行点击时
	handleCurrentChange(row, event, column) {
	  this.changeSelectData(row);
	},
	
	//全选
	selectAllChange(selection) {
	  if (this.selectedlist.length == 0) {
	    this.selectedlist = selection.map(s=>s)
	  } else {
	    if (selection.length == 0) {
	      this.table.forEach(res => {
	        const index = this.selectedlist.findIndex(s=>s.id == res.id);
	        if (index > -1) {
	          this.selectedlist.splice(index, 1);
	        }
	      });
	    } else {
	      selection.forEach(res => {
	        const index = this.selectedlist.findIndex(s=>s.id == res.id);
	        if (index == -1) {
	            this.selectedlist.push(res);
	        }
	      });
	    }
	  }
	},
	
	// 选中存储方法
	changeSelectData(row) {
	  if (this.selectedlist.length > 0) {
	    let same = false;
	    const index = this.selectedlist.findIndex(s=>s.id == row.id);
	    if (index > -1) {
	      same = true;
	      this.selectedlist.splice(index,1)
	    } else {
	      this.selectedlist.push(row);
	    }
	    this.$refs.multipleTable.toggleRowSelection(row, !same);
	  } else {
	    this.$refs.multipleTable.toggleRowSelection(row, true);
	    this.selectedlist.push(row);
	  }
	},
	//刷新展示选中(分页请求数据,拿到数据后调用该方法)
	showSelected() {
	  this.$refs.multipleTable.$nextTick(_ => {
	    this.table.map(res => {
	      const index = this.selectedlist.findIndex(s=>s.id == res.id);
	      if (index > -1) {
	        this.$refs.multipleTable.toggleRowSelection(res, true);
	      }
	    });
	  });
	},
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值