Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

Categories

0 votes
277 views
in Technique[技术] by (31.9m points)

树状图点击子节点如何获取所有父节点数据

点击第三层的节点,如何通过js拿到第一层和第二层的节点数据,数据就像下面的这样

[
  { 
    key: '1', 
    value:'第一层', 
    children:[
        {
          key: '2', 
          value:'第二层', 
          children:[
            {
              key: '3', 
              value:'第三层', 
              children:[]
            }
    ]
  },
   { 
    key: '4', 
    value:'第一层', 
    children:[
        {
          key: '5', 
          value:'第二层', 
          children:[
            {
              key: '6', 
              value:'第三层', 
              children:[]
            }
    ]
  }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)

可以点击的时候拿到 key 值 dfs 找到子节点,再回溯找到所有父节点;

或者一开始构建一个map,再内部定义一个 _key: 1-2-3 后面直接通过 _key 查找;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

755k questions

547k answers

4 comments

86.4k users

...