ElementUI tree控件把选中的父节点下的子节点抽出组成一个新的数组
时间: 2024-04-12 14:27:35 浏览: 24
你可以使用 ElementUI tree 控件的 `getCheckedNodes()` 方法来获取选中的节点数组,然后通过遍历这个数组,找到选中节点的父节点,并把它的子节点抽出组成一个新的数组。
下面是一个示例代码:
```javascript
// 获取选中的节点数组
const checkedNodes = this.$refs.tree.getCheckedNodes();
// 定义一个空数组来存放子节点
const childNodes = [];
// 遍历选中的节点数组
checkedNodes.forEach(node => {
// 判断节点是否有父节点
if (node.parent) {
// 将子节点添加到数组中
childNodes.push(...node.parent.children);
}
});
// 去重
const uniqueChildNodes = Array.from(new Set(childNodes));
console.log(uniqueChildNodes);
```
在上面的代码中,`this.$refs.tree` 是你的 ElementUI tree 控件的引用。`getCheckedNodes()` 方法返回的是一个包含所有选中节点的数组。然后,我们遍历这个数组,通过判断节点是否有父节点,将父节点的子节点添加到 `childNodes` 数组中。最后,使用 `Set` 数据结构将数组去重,得到最终的子节点数组 `uniqueChildNodes`。
希望能帮到你!如果还有其他问题,请随时提问。