Javascript 角度ui树,从数据库中的数据填充树

Javascript 角度ui树,从数据库中的数据填充树,javascript,angularjs,arrays,tree,angular-ui-tree,Javascript,Angularjs,Arrays,Tree,Angular Ui Tree,我使用angular ui树库来显示文件夹结构 我将节点对象存储在MongoDB数据库中 每个节点对象如下所示 { "node_name" : "Folder 1", "node_path" : "AAABBB", "node_id" : 103, "node_parent_path" : "AAA", "node_parent_id" : 13, "template" : "Template 1" } 角度UI树以这种方式填充 data = [ {

我使用angular ui树库来显示文件夹结构

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

每个节点对象如下所示

{
   "node_name" : "Folder 1",
   "node_path" : "AAABBB",
   "node_id" : 103,
   "node_parent_path" : "AAA",
   "node_parent_id" : 13,
   "template" : "Template 1"
}
角度UI树以这种方式填充

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中的大量节点,我希望填充数据数组,以便能够填充UI树

最好的方法是什么


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

不确定您使用的是哪种服务器语言,因此我将对其进行概括。
您有两个不同的选项,要么是递归查询,要么是从平面列表构建嵌套列表

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

2)关联数组: 从mongo获取所有节点,将它们放入由节点路径设置关键帧的关联数组中。迭代此列表中的所有项,通过使用parent_path作为关联数组的键添加到相应父级的“nodes”列表中。然后从关联数组中按路径获取根节点,并将其分配给“数据”数组。也可以选择双链接以强制父引用

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


祝你好运

ui树是最好的选择。到目前为止你做了什么?