我使用角用户界面树库来显示文件夹结构.
我将节点对象存储在MongoDB数据库中。
每个节点对象如下所示
{
"node_name" : "Folder 1",
"node_path" : "AAABBB",
"node_id" : 103,
"node_parent_path" : "AAA",
"node_parent_id" : 13,
"template" : "Template 1"
}以这种方式填充角用户界面树。
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树。
做这件事最好的方法是什么?
或者,是否有更好的方法将这些节点存储在数据库中,以便更容易地检索该信息以填充树?
发布于 2016-08-25 23:49:32
不确定您使用的是哪种服务器语言,所以我将保持它非常一般化。
您有两个不同的选项,要么是递归查询,要么是从平面列表构建嵌套列表。
1)递归查询:编写函数以获取现有节点的所有子节点,获取根节点,并将结果作为子节点添加到现有节点,在返回的每个返回结果上运行递归查询函数。这将导致从根节点开始的适当结构。
2)关联数组:从mongo获取所有节点,并将它们放入由node_path键键确定的关联数组中。迭代此列表中的所有项,通过使用parent_path作为关联数组的键,将其添加到相应父节点的“节点”列表中。然后通过路径从关联数组中获取根节点,并将其分配给'data‘数组。您也可以选择双链接来强制执行父引用。
在答案1中,如果根实际上是一个列表,则可能需要“虚拟”根节点。在答案2中,您可能需要扫描关联数组中的所有项,以提取那些没有parent_node的项来创建基本根列表。请随时询问后续行动更多信息。
祝好运!
https://stackoverflow.com/questions/39156275
复制相似问题