簡體   English   中英

需要將復雜的json obj轉換為合適的angularjs ui樹數據json結構

[英]need to convert complex json obj to suitable angularjs ui tree data json structure

在我的angularjs項目中,我具有以下格式的原始json:

$scope.orgJsonObj = {  
  "parentNodesList":[  
    "0",
    "1",
    "1.1",
    "2",
    "2.2"
  ],
  "childNodes":{  
    "0":[  
      "1",
      "2"
    ],
    "1":[  
      "1.1",
      "1.2"
    ],
    "1.1":[  
      "1.1.1"
    ],
    "2":[  
      "2.1",
      "2.2"
    ],
    "2.2":[  
      "2.2.1",
      "2.2.3"
    ]
  },
  "nodeDetailsList":[  
    {  
      "id":"0",
      "name":"node0"
    },
    {  
      "id":"1",
      "name":"node1",
      "parentId":"0"
    },
    {  
      "id":"2",
      "name":"node2",
      "parentId":"0"
    },
    {  
      "id":"1.1",
      "name":"node1.1",
      "parentId":"1"
    },
    {  
      "id":"1.2",
      "name":"node1.2",
      "parentId":"1"
    },
    {  
      "id":"1.1.1",
      "name":"node1.1.1",
      "parentId":"1.1"
    },
    {  
      "id":"2.1",
      "name":"node2.1",
      "parentId":"2"
    },
    {  
      "id":"2.2",
      "name":"node2.2",
      "parentId":"2"
    },
    {  
      "id":"2.2.1",
      "name":"node2.2.1",
      "parentId":"2.2"
    },
    {  
      "id":"2.2.3",
      "name":"node2.2.3",
      "parentId":"2.2"
    }
  ]
}

現在我想將orgJsonObj json結構轉換為類似於angular ui tree json數據結構的結構。 orgJsonObjparentNodesList包含樹中的所有父級。 每個父級的子級列表在childNodes中都可用。 每個節點的完整信息可在nodeDetailsListnodeDetailsList 節點obj { "id":"0", "name":"node0"}在其中沒有parentId屬性,因為它是樹的根。

轉換后,我的$scope.orgJsonObj應當如下所示(適用於angularjs ui樹)

$scope.finalJsonObj = [  
  {  
    "id":"0",
    "title":"node0",
    "nodes":[  
      {  
        "id":"1",
        "title":"node1",
        "nodes":[  
          {  
            "id":"1.1",
            "title":"node1.1",
            "nodes":[  
              {  
                "id":"1.1.1",
                "title":"node1.1.1",
                "nodes":[  

                ]
              }
            ]
          },
          {  
            "id":"1.2",
            "title":"node1.2",
            "nodes":[  

            ]
          }
        ]
      },
      {  
        "id":"2",
        "title":"node2",
        "nodes":[  
          {  
            "id":"2.1",
            "title":"node2.1",
            "nodes":[  

            ]
          },
          {  
            "id":"2.2",
            "title":"node2.2",
            "nodes":[  
              {  
                "id":"2.2.1",
                "title":"node2.2.1",
                "nodes":[  

                ]
              },
              {  
                "id":"2.2.3",
                "title":"node2.2.3",
                "nodes":[  

                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

誰能幫我這個忙。

var originalData = {};

var treeData = transformNesting("0", originalData.childNodes);
var result = applyData(treeData, originalData.nodeDetailsList);

function transformNesting(root, nestedData){
    var tree = {
        id: root,
        nodes: nestedData[root] ? nestedData[root].map(function(newRoot){ return transformNesting(newRoot, nestedData)}) : []
    };
    return tree;
}

function getNodeById(list, id){
    return list.filter(function(item){
       return item.id === id;
    })[0];
}

function applyData(tree, config){
    tree.title = getNodeById(config, tree.id).name;
    tree.nodes =  tree.nodes.map(function(node){return applyData(node, config)});
    return tree;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM