VUE根据子节点查询父节点 适用于el-cascader组件赋值
getFathersById(id, data, prop = "value") {var arrRes = [];const rev = (data, nodeId) => {for (var i = 0, length = data.length; i < length; i++) {const node = data[i];if (node[prop] === nodeId) {
·
getFathersById(id, data, prop = "value") {
var arrRes = [];
const rev = (data, nodeId) => {
for (var i = 0, length = data.length; i < length; i++) {
const node = data[i];
if (node[prop] === nodeId) {
arrRes.unshift(node[prop]);
return true;
} else {
if (node.children && node.children.length) {
if (rev(node.children, nodeId)) {
arrRes.unshift(node[prop]);
return true;
}
}
}
}
return false;
};
id:子节点
data:数据源
prop:默认根据哪个值查询
更多推荐
已为社区贡献9条内容
所有评论(0)