- 场景:
后台返回数据,进入页面编辑的时候,一些表格数据选中; - 解决办法:
调用后台接口的返回数据之后,使用table的 toggleRowSelection 方法实现默认勾选。
data(){
imageList:[], //后台接口返回的列表数据
selectList:[], //已经选中的数据,[{},{},{}]},每一项包含很多其他参数
//调用接口成功后执行
this.$nextTick(()=>{
this.imageList.forEach(row=>{
this.selectList.forEach(selected =>{
if(selected.id === row.id){
this.$refs.multipleTable.toggleRowSelection(row,true);
}
})
})
})
上面写法在实际使用中发现了问题,就是只能选中默认的第一页,之后其他页的多选框没有选中;但是this.showSelectList中已经读取到从编辑接口获取到的数据。
解决办法:
在点击入口事件的时候,就开始多选绑定,绑定发生在调用接口之前。
props:{
parImageList:{//父组件传来的
type:Array,
},
}
// 获取回显数据列表
if(this.parImageList){
let value = JSON.stringify(this.parImageList)
this.selectList = JSON.parse(value)
}
// 多选默认选中
this.$nextTick(()=>{
this.selectList.forEach(item=>{
this.$refs.multipleTable.toggleRowSelection(item);
})
})
// 获取数据列表
this.getListFunc();