el-table翻页记忆勾选

该文章介绍了一个在Vue.js应用中处理分页表格的场景,如何实现在不同页面间切换时保持用户批量删除功能中所选中的行。通过使用`el-table`的`reserve-selection`属性和`selection-change`事件,结合`$refs`来管理选中状态,并在数据变更时同步选中项。

表格是分页的,要做一个批量删除功能,第一页勾选的数据到了第二页再回到第一页依然要保留勾选的数据,实现如下:

<el-table ref="tableRef":data="tableList" :row-key="getRowKey" @selection-change="handleSelectionChange">
	<el-table-column fixed="left" type="selection" width="45" :reserve-selection="true" />
	<el-table-column label="用户名" props="userName"></el-table-column>
<el-table>
<script>
export default {
	data() {
		return {
			tableList: [],
			selectList: [] //勾选的数据
		}
	},
	
	mounted: {
		this.getTableList();
	},
	
	methods: {
		getTableList() {
			let data = [{userName: "白小白"}];
			this.tableList = data;
			this.$nextTick(() => {
	           this.setSelectRow();
	        });
		},
		//勾选
	    handleSelectionChange(val) {
	      this.selectedList = val;
	    },
	    
	    //取消多选
	    cancelMultiple() {
	      this.$refs.tableRef.clearSelection();
	    },
	
	    // 批量操作的id
	    getRowKey(row) {
	      return row.id;
	    },
	    //设置选中
	    setSelectRow() {
	      let ids = this.selectedList.map((item) => item.id);
	      if (this.tableList && this.tableList.length) {
	        this.tableList.forEach((item) => {
	          if (ids.length > 0) {
	            if (ids.indexOf(item.id) !== -1 && item) {
	              this.$refs.tableRef.toggleRowSelection(item, true);
	            }
	          } else {
	            this.$refs.tableRef.toggleRowSelection(item, false);
	            this.cancelMultiple();
	          }
	        });
	      }
	    },
	}
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值