2013-04-16 60 views
1

本例中爲基地的所有小孩: http://mbostock.github.io/d3/talk/20111018/tree.htmlD3樹佈局:我想使用jQuery來切換我使用

在我的代碼我開始與所有節點崩潰,用戶可以點擊跳轉到瀏覽樹在演示中。

爲了方便用戶,我想用jQuery來切換展開/摺疊根的所有子項。下面的代碼只會切換根目錄的直接子項。

我已經嘗試了很多選擇,但是我無法解決正確的功能。任何幫助,將不勝感激。

$('.clicktoexpandALL').click(function(){ 
    toggle(root); 
    update(root); 
}); 

我已經試過toggle(root.children[0]) & toggle(root.children[1].children[2]);無濟於事。

編輯:更新的問題。

如果我可以訪問toggleAll(d)函數,我將能夠做我想做的事情,但一個簡單的函數調用將無法正常工作。

d3.json( 「JSON/results.json」,函數(JSON){ 根= JSON; root.x0 = H/2; root.y0 = 0;

function toggleAll(d) { 
    if (d.children) { 
    d.children.forEach(toggleAll); 
    toggle(d); 
    } 
} 

// Initialize the display to hide nodes. 
root.children.forEach(toggleAll); 

update(root); 

} );

ADDED的jsfiddle LINKS

與代碼 http://jsfiddle.net/chrisloughnane/vV3Sc/

全屏 http://jsfiddle.net/chrisloughnane/vV3Sc/show/

回答

1

我認爲你需要使用的方法toggleAll,其中切換根節點和其下的子節點:

function toggleAll(d) { 
    if (d.children) { 
    d.children.forEach(toggleAll); 
    toggle(d); 
    } 
} 

toggle方法將隱藏/顯示根的屬性children,但不顯示其他節點的子項。

+0

不幸的是,我不能直接調用該方法。我會在早上再次入住。 –