vue根据树节点的id查找树节点的label,递归解决

最近有一个需求是根据树节点的id查找树节点的label,其实这种应该后端直接把id和label传过来就行了,就不用使用多余的操作来降低系统性能了,但是既然已经有了这个需求,我们就来解决吧!

首先,你得有树节点的数据:

// 使用示例
      const treeData = [
        {
          id: 2,
          name: '节点2',
          children: [
            {
              id: 4,
              name: '节点4',
              children: [
                { id: 6, name: '节点6' },
                { id: 7, name: '节点7' }
              ]
            },
            {
              id: 5,
              name: '节点5',
              children: [
                { id: 8, name: '节点8' },
                { id: 9, name: '节点9' }
              ]
            }
          ]
        }
      ]

然后,你就需要写一个函数对这些数据进行操作了,这个函数传递的参数有两个,一个是这整个树的数据(数组)和你要查找的id(注意你的id是字符串还是数字,类型得相同),以下是递归函数实现过程:

const searchNode = (node, nodeId) => {
        if (node.id === nodeId) {
          return node.name
        } else if (node.children !== undefined) {
          for (let i = 0; i < node.children.length; i++) {
            const result = searchNode(node.children[i], nodeId)
            if (result !== null) {
              return result
            }
          }
        }
        return null
      }

函数和数据都有了我们举个例子吧,比如要在这个树中查找id为6的label,以下是使用示例:

// 使用示例
      const nodeId = 6
      const result = searchNode({ id: 0, children: treeData }, nodeId)
      if (result !== null) {
        console.log(`包含id为${nodeId}的节点,label为${result}`)
      } else {
        console.log(`未找到id为${nodeId}的节点`)
      }

完整的js代码:

<script>
      // 使用示例
      const treeData = [
        {
          id: 2,
          name: '节点2',
          children: [
            {
              id: 4,
              name: '节点4',
              children: [
                { id: 6, name: '节点6' },
                { id: 7, name: '节点7' }
              ]
            },
            {
              id: 5,
              name: '节点5',
              children: [
                { id: 8, name: '节点8' },
                { id: 9, name: '节点9' }
              ]
            }
          ]
        }
      ]
      const searchNode = (node, nodeId) => {
        if (node.id === nodeId) {
          return node.name
        } else if (node.children !== undefined) {
          for (let i = 0; i < node.children.length; i++) {
            const result = searchNode(node.children[i], nodeId)
            if (result !== null) {
              return result
            }
          }
        }
        return null
      }

      // 使用示例
      const nodeId = 6
      const result = searchNode({ id: 0, children: treeData }, nodeId)
      if (result !== null) {
        console.log(`包含id为${nodeId}的节点,label为${result}`)
      } else {
        console.log(`未找到id为${nodeId}的节点`)
      }
    </script>

分享书摘:

“后来走廊被黄昏染色,冬天被大雪唤醒,思念被歌曲收藏,却找不到分享的人,告别就是看到所有美好的东西,也不会再和你说了”

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下方式为 Vue Tree 中的最后一个节点设置 label 颜色为红色: ```html <template> <div> <div class="tree"> <tree-node :node="root" /> </div> </div> </template> <script> import TreeNode from './TreeNode.vue'; export default { components: { TreeNode, }, data() { return { root: { label: 'Root', children: [ { label: 'Child 1', children: [ { label: 'Grandchild 1.1', }, { label: 'Grandchild 1.2', }, ], }, { label: 'Child 2', children: [ { label: 'Grandchild 2.1', }, { label: 'Grandchild 2.2', }, ], }, { label: 'Child 3', children: [ { label: 'Grandchild 3.1', }, { label: 'Grandchild 3.2', }, { label: 'Grandchild 3.3', }, ], }, ], }, }; }, }; </script> <style> .tree { margin: 0; padding: 0; } .tree-node { list-style: none; margin-left: 1em; position: relative; } .tree-node::before { content: ''; position: absolute; top: -0.4em; left: -1em; border-left: 1px solid #ccc; border-bottom: 1px solid #ccc; height: 1em; } .tree-node::after { content: ''; position: absolute; top: -0.4em; left: -1em; border-left: 1px solid #ccc; border-top: 1px solid #ccc; height: 1em; } .tree-node:last-child::before { border-left: none; } .tree-node:last-child::after { border-left: none; } .tree-node .label { display: inline-block; padding: 0.25em 0.5em; background-color: #f5f5f5; border-radius: 0.25em; } .tree-node:last-child .label { color: red; } </style> ``` 在上面的代码中,我们使用递归组件 `TreeNode` 渲染形结构,并为最后一个节点label 添加了 `color: red` 样式。具体实现方式为在 `TreeNode` 组件的 `template` 中判断当前节点是否为最后一个节点,若是则为其 label 添加样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值