在d3树中折叠加载时的特定节点

时间:2015-05-22 08:30:38

标签: jquery d3.js tree collapse expand

你好我更新我的问题还包括一个工作的jsfiddle,以清楚地看到我想做什么.... http://jsfiddle.net/elviz/Ge58Q/15/

 nodeEnter.append("text")
   .attr("dy", 3.5)
   .attr("dx", 5.5)
   .attr("x", barWidth +20)
   .text(function(d) { return d.redirect_info; })
   .on("click", function(d){
        // using the value in d.redirect
       //search for the node with item_id equal to d.redirect
       //after finding that node input in update to draw



   });

上面的代码我创建了一个文本并附加一个onclick事件......如果文本是单击我想要的是使用d.redirect的值查找item_id等于d.redirect然后输入的节点更新函数中的数组用于绘制树.......在我的示例中,如果单击文本redirect_9_node_simple,它将从ROOT UP TO THE NODE SIMPLE中绘制节点,如果文本redirect_13_node_alluring是click,则我想绘制树从ROOT到ALLINGING NODE。注意请向右滚动,因为文本链接是矩形右侧的20px

1 个答案:

答案 0 :(得分:1)

您可以隐藏节点的子节点,如下所示:

function collapseSingle(node) {
    if (node.children) {
        node._children = node.children;
        node.children = null;
    }
}

现在,如果您想显示节点的子节点,您可以这样做:

function expandSingle(node) {
    if (node._children) {
        node.children = node._children;
        node._children = null;
    }
}

因此,在隐藏所有子项后,您将显示节点2 - clustergraph节点的子节点:

flare.children.forEach(collapse);
flare.children.forEach(expandSingle);

flare.children只包含一个节点2,其子节点可见(参见第37和47行http://jsfiddle.net/Ge58Q/10/)。在这种情况下,行flare.children.forEach(expandSingle)可以替换为expandSingle(flare.children[0]) ... flare.children [0]是节点2

相关问题