[CLOSED] TreePanel: Expand Tree to a certain depth

  1. #1

    [CLOSED] TreePanel: Expand Tree to a certain depth

    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
    Last edited by fabricio.murta; Feb 22, 2015 at 2:20 AM. Reason: [CLOSED]
  2. #2
    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

    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!
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Thank you very much. It is perfect. Please close the thread.
  4. #4
    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 ...
  5. #5
    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)
    Fabrício Murta
    Developer & Support Expert
  6. #6
    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?
  7. #7
    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.
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. [CLOSED] tree panel can not expand in ie
    By hdsoso in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Oct 20, 2013, 10:29 PM
  2. [CLOSED] auto expand tree ?
    By hdsoso in forum 2.x Legacy Premium Help
    Replies: 27
    Last Post: May 17, 2013, 2:16 AM
  3. Replies: 1
    Last Post: Apr 02, 2013, 5:50 AM
  4. Replies: 4
    Last Post: Aug 16, 2012, 1:08 PM
  5. Tree "Expand All" and "Collapse All" icons
    By daneel in forum 1.x Help
    Replies: 1
    Last Post: Mar 18, 2010, 1:11 PM

Posting Permissions