Get All ChildNodes of a TreeNode in ExtJS - Deep Tree Search


***Getting All ChildNodes (including at all levels of a TreeNode) in ExtJS***


Hi friends,


Here is a function which I had written to get all children of a TreeNode i.e. Both immediate (can also be obtained from TreeNode.childNodes) and all innerNodes (grand, grand-grand, etc.) till leaf-level i.e. till the end all the nodes.


As said, With TreeNode's property "childNodes" we get only first level child nodes array.
Consider if you want array of all the nodes under the given node, then following function will help you.

 /**
  * Following Function recurses through all nodes under the given node,
  * and return an Array of AsyncTreeNode objects (containing itself)
  * (Note - If the given Object is not desired in returned array can be easily
  *  removed, using splice/slice methods of JS Array methods)
  */

  getDeepAllChildNodes = function(node){ 
        var allNodes = new Array(); 
        if(!Ext.value(node,false)){ 
                return []; 
        } 
        
        if(!node.hasChildNodes()){ 
                return node; 
        }else{ 
                allNodes.push(node); 
                node.eachChild(function(Mynode){allNodes = allNodes.concat(getDeepAllChildNodes(Mynode));});         
        } 
        return allNodes; 
}

//How To Use - 
//Consider I have a AsyncTreeNode/TreeNode - and want to get All its child nodes Array then

  var allChildNodes = getDeepAllChildNodes(Node);
  //OR
  var allChildNodes = getDeepAllChildNodes(Ext.getCmp(treePanelId).getNodeById(nodeId));
  //OR to get All nodes in a Tree
  var allChildNodes = getDeepAllChildNodes(Ext.getCmp(treePanelId).getRootNode());

Fill free to ask if you have any difficulty or didn't understand anything...

Comments

Popular posts from this blog

How to install / Configure Mantis For Apache-PHP-PostgreSQL combination

Modified ExtJS LOVCombo (List of Values) ux plugin - with select all, deselect all, text filter, bubble up and bubble down of selection

TriggerField with two triggers, TwinTriggerField with tooltips