开发者

Expanding all nodes in dijit.Tree

Is there a good way to expand/close all the expandable nodes in a dijit.Tree?

For those looking for an answer, put this in your initializing code:

var treeControl = new dijit.Tree({
    model: treeModel,
    expandAll: function() {
        // summary开发者_JAVA百科:
        //     Expand all nodes in the tree
        // returns:
        //     Deferred that fires when all nodes have expanded

        var _this = this;

        function expand(node) {
            _this._expandNode(node);

            var childBranches = dojo.filter(node.getChildren() || [], function(node) {
                return node.isExpandable;
            });

            var def = new dojo.Deferred();
            defs = dojo.map(childBranches, expand);
        }
        return expand(this.rootNode);
    }
});

At least, that works for me. And you can do the same with collapseAll() where you only need to switch _this._expandNode(node); with _this._collapseNode(node);


Yes, autoExpand=true (as an initialization parameter to the Tree).

If you need to expand/collapse dynamically, Tree used to have a method for that, but I took it out. However, you can copy it from: http://bugs.dojotoolkit.org/changeset/20529.


To collapse all nodes... ( remember to NOT collapse the root node when it is not shown( I like to have multiple items shown for my trees ))

_collapseAllTreeNodeContainers: function(){ 

     var _tree = _this; 

        function collapse(node) {
   // never collapse root node, otherwise hides whole tree !
   if ( _tree.showRoot == false && node != _tree.rootNode ) {
    _tree._collapseNode(node);
   }

            var childBranches = dojo.filter(node.getChildren() || [], function(node) {
                return node.isExpandable;
            });

            var def = new dojo.Deferred();
            defs = dojo.map(childBranches, collapse);
        }
        return collapse( _tree.rootNode);
    }


You could use on the dijit/Tree instance the following methods:

tree.expandAll();

http://dojotoolkit.org/api/?qs=1.10/dijit/Tree#1_10dijit_Tree_expandAll

or

tree.collapseAll();

http://dojotoolkit.org/api/?qs=1.10/dijit/Tree#1_10dijit_Tree_collapseAll

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜