我正在尝试从文件路径创建树视图,例如可以添加和删除文件路径:
$(document).on('click', '#on', function(){
$('#chqMsgContent').html('Total Diffrent than the Recive Amount');
...
setTimeout(function(){
$("#okDif").focus();
}, 150);
});
但是,我的树要求在一个节点中折叠没有子项(文件)的路径。对于上面的路径,它将产生:
A/B/C/D/file1.txt
A/B/D/E/file2.txt
A/B/D/G/file3.txt
A/B/D/G/file4.txt
有什么想法?创建树很容易,但我无法超越那个额外的条件...我假设我必须使用某种递归,因为我发现添加项目并打破路径,因为我们发现某条路径有更多的孩子(然后递归地做同样的事情?)。我应该使用某种特里?当相同的路径可以有多个文件时它会工作吗?...谢谢!
答案 0 :(得分:0)
让我们从一个简单的解决方案开始,实际打印树:
function browseTree(node)
{
// ...print node...
// Visit recursively the children nodes:
for (var child: node.children)
{
browseTree(child);
}
}
现在,让我们将其修改为"缩短"单文件夹路径:
function browseTree(node)
{
// Before printing, accumulate as many straight folders as possible:
var nodeName=node.name
while (hasJustOneFolder(node))
{
// This loop steps deeper in the tree:
node=node.children[0]
nodeName+="/"+node.name;
}
// ...print node...
// Last, visit recursively the non-unique children nodes:
for (var child: node.children)
{
browseTree(child);
}
}
function hasJustOneFolder(node)
{
return node.children.length==1 && node.children[0].isFolder();
}