PDA

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



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

ExpandDepth(1) would look like:



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


ExpandDepth(2) would look like:


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

fabricio.murta
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!

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

cwolcott
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 ...

fabricio.murta
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)

cwolcott
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?

fabricio.murta
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)).