D3.js Tree Expand All and Collapse All

前端 未结 3 2560
再見小時候
再見小時候 2021-02-20 07:15

I\'m building a Tree using D3.js and what I am trying to do is add two buttons, Expand All and Collapse All to the top of the page like this.

3条回答
  •  轮回少年
    2021-02-20 07:51

    just add this

                       function collapse(d) {
                            if (d.children) {
                              d._children = d.children;
                              d._children.forEach(collapse);
                              d.children = null;
                            }
                          }
    
                          function expand(d) {
                            if (d._children) {
                                d.children = d._children;
                                d.children.forEach(expand);
                                d._children = null;
                            }
                          }
    
    
                                $("#expand_button").click(function(){
                                    source.children.forEach(expand);
                                    update(source);
                                });
    
    
                                $("#collapse_button").click(function(){
                                    root.children.forEach(collapse);
                                    update(root);
                                });
    

提交回复
热议问题