View Full Version : [CLOSED] TreePanel: Expand Tree to a certain depth

Feb 21, 2015, 2:56 AM
Is there any sample code to expand a tree to a certain depth?

ExpandDepth(1) would look like:

B (Has No Children)

ExpandDepth(2) would look like:

A.2 (Has No Children)
B (Has No Children)
C.1 (Has No Children)

Feb 21, 2015, 3:59 AM
Hello, Chris Wolcott!

There was no such example as far as I know. At least before now:

var rn = App.TreePanel1.getRootNode();
var expRec = function(depth, base) {
if (!base) base = rn;
if (depth < 1) return;
if (!base.isExpanded()) base.expand()
Ext.each(base.childNodes, function(node) { expRec(depth - 1, node); });

I developed this using this example: Multi Node TreePanel built using markup (http://examples2.ext.net/Examples/TreePanel/Basic/Built_in_Markup/)

Just call exprec(depth) and have your tree expanded down to that level. This is a recursive approach. You can also write the same in a loop approach if you're concerned with function recursion overhead.

I hope this really does what you expected it to!

Feb 22, 2015, 3:17 AM
Thank you very much. It is perfect. Please close the thread.

Feb 22, 2015, 3:52 AM
After testing I changed one thing.

if (depth < 1) { base.collapse(); return; };

I expanded the tree to level 3, then tried to expand back to level 2 and thus needed to collapse anything below that. The quick way was to first collapseAll(), but it did not look smooth. Adding the new code made the tree animation look better.

Thanks again for the code ...

Feb 22, 2015, 4:37 PM
Thanks for sharing this increment to the code, I was really just thinking in the expanding scenario. With that, one can switch the tree to any depth deeper or shallower.

Just watch for big trees, this code might add a heavy strand to the browser in such scenarios. :)
(this applies to both expanding and expand/collapse approaches)

Feb 22, 2015, 4:57 PM
Why would you say that. It is a pretty simplistic method. Is your concern in breadth / depth or both?

Having 50 nodes with 1 child each or having 17 nodes with a depth of 3 each?

Just trying to understand your concerns. Because wouldn't expand all also have similar concerns?

Feb 22, 2015, 9:51 PM
Just thinking on a scenario when you have a tree with 5000+ nodes. :)

While the method is simple as written it weaves a huge net if you bring enough nodes. After all, it has recursion (http://en.wikipedia.org/wiki/Recursion_(computer_science)).