预期效果如下图:
实际在做的过程中,出现选中无法正常显示的问题,通过@change观测,选中值有变化,只是界面不显示。
先后尝试,均无效:
1.为el-select添加ref;
2.使用$set赋值;
3.使用v-model=“tableData[scopo.$index].postIds”
最后找到解决方法,可是形成这种现象的原因,自己不理解,欢迎知道问题所在的朋友留言。
出问题的写法:
listAuditConfigPost({ configId: item.id }).then((res) => {
this.tableData= res.data;
// 自定义属性,用于el-select , 选中部门
this.tableData.forEach((temp, index) => {
temp["postIds"] = temp.configPostList.map((post) => post.postId);
});
});
修改后,可以正常显示的写法:
listAuditConfigPost({ configId: item.id }).then((res) => {
// 1. 直接将新增用于绑定el-select的自定义字段,添加在res.data中
res.data.forEach((temp, index) => {
temp["postIds"] = temp.configPostList.map((post) => post.postId);
});
// 2.绑定后,赋值
this.tableData= res.data;
});
然后使用el-select 的@change方法,获取最新的值,因为vue不定对数组对象进行监听,所示使用$set进行强制更新,具体方法如下:
handleConfigChange(row, index) {
this.$set(this.tableData[index], "postIds", [...row.postIds]);
}