2013-02-12 91 views
0

这是一个网球抽签,我需要能够再见,所以大多数父母有两个孩子,即。每场比赛的胜者都经历过,但在某些情况下,有一次再见,所以只有一个孩子。看到这里作为一个例子,其中有些家长的比赛有没有孩子,有的有一个:http://www.irtpa.com/index.php/realtennis/ladder/1246如何删除树状布局中的单个子节点

我不认为这样的回答可以帮助:How to remove node on tree layout D3.js?

因为它是假设一个节点的所有孩子都隐藏/删除。

我走了这么远基于上述计算器的答案,但我的大脑不能看到删除/隐藏子解决方案:

function check_children(data, parent) { 
// recurse through array and toggle/hide any Match Byes 

    if (typeof data.data != "undefined") { 
    if (data.data.bye == "byeM") { 
     toggle(parent); 
    } 
    } 

    if (data.children) { 
    check_children(data.children[0], data); 
    check_children(data.children[1], data); 
    } 

} 

function toggle(d) { 
    if (d.children) { 
     d._children = d.children; 
     d.children = null; 
    } else { 
     d.children = d._children; 
     d._children = null; 
    } 
} 
+0

我不知道我明白你在做什么?你想促进一个孩子代替父母吗?摆脱使用_children的切换行应该永久地从树中移除这些孩子。也许你可以发布一个jsFiddle和/或一些前后json对象来寻找你想要的东西? – Superboggly 2013-02-13 23:21:21

+0

我想删除只有一个孩子,而不是大多数例子显示。所以_children技巧不起作用。这是一个相当实际的代码,所以希望有人会说 - 这很容易,只是......!从这个问题中解脱出来后,我可能会尝试另一种方法,看看我是否可以在布局中使用分隔来隐藏另一个。 – PhoebeB 2013-02-14 18:45:40

+0

好的 - 所以当你调用切换(父母)时,你想要删除父母的特定孩子?或者从包含它的子项列表中删除父项? – Superboggly 2013-02-14 18:52:30

回答

0

如果你想删除你的“再见”孩子,我可能会在递归之前测试。所以像这样:

function removeByeChildren(parent) { 
    if(!parent.children) 
    return; 

    var i = 0; 
    while(i < parent.children.length) { 
    var data = parent.children[i].data; 
    if (typeof data != "undefined" && data.bye == "byeM") { 
     // remove child - could save it in _children if you wanted to ever put it back 
     var child = parent.children.splice(i,1); 
     // length of child list reduced, i points to the next child 
    } 
    else { 
     // not a bye - recurse 
     removeByeChildren(parent.children[i]); 
     // all the child's bye's are removed so go to the next child 
     i++; 
    } 
    } 
} 

你可以玩它here。这个想法是检查每个孩子是否再见。如果是我们将它从列表中删除,如果不是,我们递归并删除所有后代孩子的脚本。

+0

这看起来很有希望。早上起床。 – PhoebeB 2013-02-14 21:05:38