【问题标题】:Expand Tree to a specific level将树扩展到特定级别
【发布时间】:2016-10-17 07:00:13
【问题描述】:

如何使用node.setExpanded(true); 仅将树扩展至特定级别?

我的意思是我有 6 个深度级别,只希望扩展 5 个——最后一个应该被排除在外。

【问题讨论】:

  • 一个传递剩余级别计数的递归函数可以,但您确实需要显示您尝试过的代码:)

标签: jquery fancytree


【解决方案1】:
    $("#treeView").fancytree("getRootNode").visit(function(node){
        if(node.getLevel() < 3) {
            node.setExpanded(true);
        }
    });

【讨论】:

  • 最好解释一下这段代码如何帮助 OP,尤其是在这个两年前的问题上已有一个公认的答案。
  • 当时这很重要,但感谢您的方法 :-) 也许它也对其他人有帮助 :-)
【解决方案2】:

这只是一个指南,因为您没有提供示例数据或代码,但您可以使用递归函数来迭代树,当达到特定深度时停止。比如:

function expandNode(node, depth) {
    // Expand this node
    node.setExpanded(true);
    // Reduce the depth count
    depth--;
    // If we need to go deeper
    if (depth > 0) {
        for (var i = 0; i < node.children.length; i++) {
            // Go recursive on child nodes
            expandNode(node.children[i], depth);
        }
    }
}

并用根节点和整体深度调用它:

expandNode(rootNode, 5);

可能有更好的方法来迭代fancytree,但我之前没有使用过fancytree

【讨论】:

  • 非常感谢,很抱歉没有提供任何代码。我用过 $("#tree").fancytree("getRootNode").visit(function(node){ node.setExpanded(true); });用于扩展从根节点开始的每个节点!
  • @Andreas Zeiner:代码和示例 html 本来意味着一个工作演示,但我希望你能从这个例子中得到想法:)
  • 工作完美!你是明星,愿最后一位忍者与你同在 :-) 是的,他们都在 c64 上玩过——自 1985 年以来一直在那里!
  • 你也可以使用node.visit(...)if( node.getLevel() &lt; 6)...
  • @mar10:这是我不知道的fancytree 特定的东西。你为什么不发布答案?
猜你喜欢
  • 1970-01-01
  • 2021-12-05
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
相关资源
最近更新 更多