element UI的Cascader 级联选择器在树形列表中编辑使用

今天一个设计是在树形列表中编辑表单,其中有Cascader 级联选择器 级联选择器的选择结果一般都是数组,而后端参数只要最后一级的id,假如只传最后一级id,那回显可能就会有问题;

解决办法是:多传一个参数,

参数A:1(后端要的id);

参数B:1,2,3,4,选择后的完整数组(自己使用,用于回显),
需要注意的是传给后端的时候需要转换成字符串,回来的时候再转换成数组使用

代码如下:

html 复制代码
<el-cascader v-model="row.responsibleDeptPath" :options="deptList" :props="treeOption"></el-cascader>
javascript 复制代码
<script>
    export default {
        data (){
            return {
                deptList: [],
                treeOption: {
                   checkStrictly: true,
                   label: 'title',
                   value: 'id',
                },       
            }
        },
        created(){
            const aa = [
    {
        responsibleDept: 1892533,
        responsibleDeptPath: "[\"1837608\",\"1837616\",\"1892533\"]",
        andonAbnormalTypeVOList: [
            {
                responsibleDept: 1892533,
                responsibleDeptPath: "[\"1837608\",\"1837616\",\"1892533\"]",
                andonAbnormalTypeVOList: [
                    {
                        responsibleDept: 1892533,
                        responsibleDeptPath: "[\"1837608\",\"1837616\",\"1892533\"]",
                    }
                ]
            }
        ]
    }
];

// 遍历树形结构并转换 responsibleDeptPath 为数组
const convertDeptPathToArray = (data) => {
    return data.map(item => {
        // 将 responsibleDeptPath 字符串解析为数组
        item.responsibleDeptPath = JSON.parse(item.responsibleDeptPath);

        // 如果有子项,递归处理
        if (item.andonAbnormalTypeVOList && item.andonAbnormalTypeVOList.length > 0) {
            item.andonAbnormalTypeVOList = convertDeptPathToArray(item.andonAbnormalTypeVOList);
        }

        return item;
    });
};

const result = convertDeptPathToArray(aa);
console.log(result);
        }
    }
</script>
相关推荐
Java后端的Ai之路4 小时前
CAS并发控制-从CPU指令到企业级乐观锁
vue.js·人工智能·python·cas·乐观锁
jktantan14 小时前
每个节点 41 字节:把一棵百万节点树拆进 14 个 typed array
前端·javascript·vue.js·算法·typescript
小大宇17 小时前
Vue知识点-ElementPlus-06 单选+多选+下拉+日期选择
前端·vue.js·elementui
小大宇19 小时前
Vue知识点-ElementPlus-05 输入框
前端·javascript·vue.js
软件技术NINI19 小时前
基于SpringBoot和Vue的摇滚乐鉴赏网站
vue.js·spring boot·后端·spring·html·毕业设计·毕设
计算机毕设定制辅导-无忧学长20 小时前
《基于物品协同过滤的闲置书换购微信小程序设计开发》
java·vue.js·spring boot·微信小程序·uniapp·闲置书换购微信小程序·物品协同过滤算法
Dwzun20 小时前
基于SpringBoot3+Vue3的油莎豆种植管理系统
java·前端·vue.js·spring boot·后端·python·计算机网络
她说可以呀1 天前
Vue插槽
前端·javascript·vue.js
qq_448011161 天前
Vue + Jinja2两套模版与法冲突造成undefinedError
前端·javascript·vue.js
志尊宝1 天前
AI原生前端开发(039):AI辅助的Nuxt 4全栈开发
前端·vue.js·ai-native