tags:

views:

860

answers:

3

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:
        //     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);

+1  A: 

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.

Bill Keese
Is there a reason it got taken out? And also, it doesn't seem to work on initializing a Tree. We're packaging the whole dojo library, so I don't have direct access on the Tree.js file itself. However, I can create additional methods on it when creating a `new Tree`, but then `_expandNode()` doesn't exist... Any suggestions?
peirix
seems I was getting some problems with the callback function. I just removed that and did a recursive call on `expand()` and the almost identical `collapse()` according to the link you posted, and now it works.
peirix
No particular reason, adding those two methods would bloat the code and require more testing (and I thought we'd still need the expandOnLoad flag too), and it just wasn't clear that anyone wanted to expand/contract dynamically... I guess you do :-)If you file an enhancement ticket (at http://bugs.dojotoolkit.org) I'll add those methods in.
Bill Keese
A: 

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);
    }
Matt X
A: 

why is there a deferred added? this code doesn't seem to be complete...

tricycle