<el-form-item label="用户" prop="group_id">
<el-cascader
:options="roledata" 后台返回的数据,格式就是elementui级联选择器的
:props="props" 多选
clearable
v-model="form.group_id"
@change="handleChange"
:emitPath="false"
:show-all-levels="false"
>
</el-cascader>
</el-form-item>
data() return{
props: { multiple: true }, //支持多选
}
回显(一定要加this.$nextTick(() => {})),研究了一天,不加回显无效
后台返回数据格式:返回所有选中的子节点即可
// row 就是点击修改获取到的添加的值
handleUpdate(row) {
this.$nextTick(() => {
const targetValue = row.group_id.split(",");
this.form.group_id = targetValue.map((item) =>
this.getParentsById(this.roledata, item) //this.roledata 后台返回的数据
);
});
}
// 这是找的别人的方法,找不到原地址了
getParentsById(list, id) {
for (let i in list) {
if (list[i].value == id) {
//查询到就返回该数组对象的value
return [list[i].value];
}
if (list[i].children) {
let node = this.getParentsById(list[i].children, id);
if (node !== undefined) {
//查询到把父节把父节点加到数组前面
node.unshift(list[i].value);
return node;
}
}
}
},