2013-02-12 90 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