element el-tree循环遍历树形结构,并动态赋值disabled属性

凌晨3点,功夫不负有心人,已经累趴,效果终于出来:

贴上代码:

<style scoped>
.form {
    
  width: 50%;
}
</style>

<template>

<div class="form">

<el-tree
  :data="data2"
  show-checkbox
  node-key="id"
  default-expand-all
  :props="defaultProps">
</el-tree>
</div>

</template>

<script>
export default
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-tree 是基于 Element UI 实现的树形组件,你可以通过以下方式实现遍历到最后一层并且每个节点都加上 `disabled` 属性: 1. 首先,你需要获取 el-tree 的根节点。 2. 然后,你可以使用递归的方式遍历树形结构,对于每个节点,都添加 `disabled` 属性。 3. 最后,你需要调用 el-tree 的 `updateKeyChildren` 方法来更节点状态,使其生效。 以下是示例代码: ```javascript // 获取 el-tree 的根节点 const root = this.$refs.tree.store.rootNode; // 遍历树形结构 function traverse(node) { if (!node) { return; } // 如果节点没有子节点,说明是最后一层节点,添加 disabled 属性 if (!node.childNodes || node.childNodes.length === 0) { node.data.disabled = true; } // 遍历子节点 if (node.childNodes) { node.childNodes.forEach(childNode => traverse(childNode)); } } // 遍历根节点 traverse(root); // 更节点状态 this.$refs.tree.store.updateKeyChildren(root.key, root.childNodes); ``` 在这个示例代码中,我们首先通过 `this.$refs.tree.store.rootNode` 获取el-tree 的根节点。然后,我们定义了 `traverse` 函数,使用递归的方式遍历树形结构,对于每个节点,都添加 `disabled` 属性。最后,我们调用了 `this.$refs.tree.store.updateKeyChildren` 方法来更节点状态,使其生效。 请注意,`updateKeyChildren` 方法需要传入根节点的 `key` 和子节点数组,因此我们需要使用 `root.key` 和 `root.childNodes` 来调用该方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值