1:问题描述:
最近开发一个表格实现多选需求,但切换分页时之前勾选的取消啦,并且要编辑时默认选中需求~
2:解决方法
html
<el-table :data="dataList" ref="multipleTable" :row-key="id" @selection-change="handleSelectionChange">
<el-table-column type="selection" :reserve-selection="true"> </el-table-column>
:reserve-selection="true" 是实现跨页多选记忆的条件~~~~
3:接下来解决默认勾选之前选择的数据
<script>
// 获取列表以后默认选中之前勾选的数据的逻辑
defaultSelectedList() {
this.$nextTick(() => {
this.dataList.forEach((item, index) => {
this.selectedList.forEach((val, k) => {
if ( this.selectedList[k].id === item.id) {
this.$refs.multipleTable.toggleRowSelection(this.dataList[index], true);
}
});
});
});
}
</script>
说明:
1: dataList 代表获取当前页的列表
2: selectedList 代表之前选中的列表(本地存储/请求后端接口获取)
3:实现选中的必要代码: this.$refs.multipleTable.toggleRowSelection(this.dataList[index], true);