iView Cascader 根据子节点获取所有父节点id数组 级联选择器回显问题(根据子节点id递归获取所有父类id的数组)

级联选择器一般我们存储的只有最后一位,但是回显的时候需要整个目录的节点value值,这时候需要遍历获取父节点的value值

var path = [];

function getPath(tree, id, path) {
    for (var i = 0; i < tree.length; i++) {
        const node = tree[i];
        if (node.value == id) {
            path.push(node.value)
            return path
        } else {
            if (node.children && node.children.length) {
                if (getPath(node.children, id, path)) {
                    path.push(node.value)
                    return path
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/xiansibao/article/details/129872590