iView Cascader obtains all parent node id arrays based on child nodes cascade selector echo problem (according to child node id recursively obtains the array of all parent class ids)

Cascading selectors usually only store the last bit, but when echoing, the node value of the entire directory is needed. At this time, it is necessary to traverse to obtain the value of the parent node

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
                }
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/xiansibao/article/details/129872590
Recommended