el-table toggleRowSelection被动触发select/selection-change事件的解决方案
场景:接口返回的数据需要列表默认选中,需要调用toggleRowSelection方法,但由于非当前页数据不在数据项内,toggleRowSelection默认触发select事件导致多选数组非当前页数据被干掉
解决方案:加锁
rowSelectFlag: false // 禁止toggleRowSelection默认触发handleSelectionChange
handleSelectionChange(val) {
if (this.rowSelectFlag) return
this.multipleList = val
},
setRowSelected() {
setTimeout(() => {
this.rowSelectFlag = true
this.dataList.forEach(row => {
const matchedIndex = this.multipleList .findIndex(item => item.id === row.id)
this.$refs['modal_table'].toggleRowSelection(row, matchedIndex != -1)
})
this.rowSelectFlag = false
}, 0)
},