首先在使用toggleRowSelection的前面加nextTick
this.$nextTick(() => {
this.$refs.multipleTable.toggleRowSelection(element);
});
遇到的问题:
回显勾选一两秒之后,勾选又取消了
应该默认勾选一条数据,之后手动勾选了一条数据,@selection-change方法 控制台打印出来是两条数据,但是界面上没有回显成功
this.$nextTick(() => {
data.forEach((row) => {
this.$refs.multipleTable.toggleRowSelection(row);
});
});
原因是 data数据是我之前选中的行数据,数据没有改变。fileList是表格的所有数据,按下面这种写法就可以解决
解决方法一
this.$nextTick(() => {
this.fileList.forEach((element) => {
data.forEach((row) => {
if (element.id == row.id) {
this.$refs.multipleTable.toggleRowSelection(element);
}
});
});
});
解决方法二
el-table中加入row-key,type="selection"中加入:reserve-selection=“true”,reserve-selection为 true 则会在数据更新之后保留之前选中的数据
<el-table
ref="multipleTable"
:data="fileList"
@selection-change="handleSelect"
row-key="id"
>
<el-table-column
type="selection"
width="55"
:reserve-selection="true"
>
this.$nextTick(() => {
data.forEach((row) => {
this.$refs.multipleTable.toggleRowSelection(row);
});
});
这样就可以直接用data数据
但这种方法也有一个问题,就是选中的数量会重复
解决方法是在离开table所在的页面时,清空选中项
this.$nextTick(() => {
this.$refs.multipleTable.clearSelection();
});