1

我正在使用 angular-ui-tree 库来显示文件夹结构。

我将节点对象存储在 MongoDB 数据库中。

每个节点对象看起来像这样

{
   "node_name" : "Folder 1",
   "node_path" : "AAABBB",
   "node_id" : 103,
   "node_parent_path" : "AAA",
   "node_parent_id" : 13,
   "template" : "Template 1"
}

Angular-UI-TREE 以这种方式填充

data = [ {
           "node_name" : "Folder 1",
           "node_path" : "AAABBB",
           "node_id" : 103,
           "node_parent_path" : "AAA",
           "node_parent_id" : 13,
           "nodes" : [
                        {
                           "node_name" : "Folder 1-1",
                           "node_path" : "AAABBBAAA",
                           "node_id" : 10351,
                           "node_parent_path" : "AAABBB",
                           "node_parent_id" : 103,
                           "nodes" : [
                                        {
                                           "node_name" : "Folder 1-1-1",
                                           "node_path" : "AAABBBAAAAAA",
                                           "node_id" : 415,
                                           "node_parent_path" : "AAABBBAAA",
                                           "node_parent_id" : 10351,
                                           "nodes" : []      
                                         }
                                     ]
                         }, 
                         {
                           "node_name" : "Folder 1-2",
                           "node_path" : "AAABBBBBB",
                           "node_id" : 103531,
                           "node_parent_path" : "AAABBB",
                           "node_parent_id" : 103,
                           "nodes" : [

                                     ]
                         }, 
                     ]
         }, 
         {

           "node_name" : "Folder 2",
           "node_path" : "AAACCC",
           "node_id" : 104,
           "node_parent_path" : "AAA",
           "node_parent_id" : 13,
           "nodes" : []    
         }
]

有了这些数据,树看起来像

Folder 1 
|
---> Folder 1-1
     |
     ---> Folder 1-1-1
|
---> Folder 1-2
Folder 2

从使用如上所示模式存储在 mongoDB 中的一堆节点中,我想填充 DATA 数组以便能够填充 UI 树..

最好的方法是什么?

或者有没有更好的方法将这些节点存储在数据库中,以便更轻松地检索该信息以填充树?

4

1 回答 1

1

不确定您使用的是哪种服务器语言,所以我会保持非常笼统。
您有几个不同的选项,要么是递归查询,要么是从平面列表构建嵌套列表。

1)递归查询: 编写一个函数,获取一个现有节点的所有子节点,获取根节点,并将结果作为子节点添加到现有节点,在返回的每个返回结果上运行递归查询函数。这将导致从根节点开始的适当结构。

2) 关联数组: 从 mongo 中获取所有节点,将它们放入以 node_path 为键的关联数组中。遍历此列表中的所有项目,通过使用 parent_path 作为关联数组的键添加到适当父项的“节点”列表。然后通过路径从关联数组中获取根节点并将其分配给“数据”数组。您也可以选择使用双链接来强制执行父引用。

在答案 1 中,如果根节点实际上是一个列表,您可能需要“虚拟”根节点。在答案 2 中,您可能需要扫描关联数组中的所有项目以提取没有 parent_node 的项目以创建基本根列表。随时询问更多信息的后续行动

祝你好运!

于 2016-08-25T23:49:32.813 回答