从angularJS树视图中获得的叶节点的深度节点、视图、深度、angularJS

2023-09-13 04:25:54 作者:啊哈—

我有一个树形视图JSON对象是

I have a tree view Json object is

{
  "Name": "Root",
  "Id": "-1",
  "ParentId": "null",
  "children": [
    {
      "Name": "Math",
      "Id": "1",
      "ParentId": "-1",
      "children": [
        {
          "Name": "Addition",
          "Id": "3",
          "ParentId": "1",
          "children": [
            {
              "Name": "Two Numbers",
              "Id": "5",
              "ParentId": "3",
              "children": []
            },
            {
              "Name": "Three Numbers",
              "Id": "6",
              "ParentId": "3",
              "children": []
            }
          ]
        },
        {
          "Name": "Substraction",
          "Id": "4",
          "ParentId": "1",
          "children": [
            {
              "Name": "Four Numbers",
              "Id": "7",
              "ParentId": "4",
              "children": []
            },
            {
              "Name": "Five Numbers",
              "Id": "8",
              "ParentId": "4",
              "children": []
            }
          ]
        }
      ]
    },
    {
      "Name": "English",
      "Id": "1",
      "ParentId": "-1",
      "children": []
    }
  ],
  "selected": "selected"
}

如果我选择双号叶节点,mytree.currentNode.Name只显示双号,但我想从叶节点的深度显示。 根/数学/加/双号有什么办法来显示?结果请帮帮我。在此先感谢

If i select "Two numbers" leaf node, mytree.currentNode.Name only shows "Two Numbers", But i want to display from the depth of the leaf node. "Root / Math / Addition / Two Numbers" Is there any way to display? Please help me. Thanks in advance

推荐答案

这是设计descision,你花正在你很难得到整个深度。

It's the design descision that you took is making it hard for you to get the entire depth.

有是存储分层数据的方法有两种。无论是使用引用或使用ID。不要混搭。

There is two ways of storing hierarchical data. Either use reference or use ID. Do not mix and match.

相反的ParentId的,如果你把父母的参考,那么你会很轻易就能走下来整个树。

Instead of ParentId, if you keep the reference of parent, then you would be very easily able to walk down entire tree.

如果你想使用父母身份证,然后代替嵌套的儿童使用ID和对象的字典。这样一来,你也可以很容易地(和快速)遍历树

If you want to use parent ID, then instead of nested children use a dictionary of ID and object. In that way also you could very easily (and fast) traverse the tree