# Expand TreeNode till certain Depth

1. ## Expand TreeNode till certain Depth

Hi,
This is the code to expand the treenode to certain depth, i have written 2 functions, in which one function gets recursively called till it completes expansion of given node till its given depth or till its leaf node
Code:
```//This Function recurses through everynode till conditions are met
//Currently Depth is hard-coded to 4 but can be given a variable value to change as per situation
myfunction = function(Mynode){
if(Mynode.hasChildNodes() == false || Mynode.getDepth() >= 4){
return;
}else if(Mynode.hasChildNodes()){
Mynode.expand(false);
Mynode.eachChild(myfunction);
}
}

//this Function expands given node till depth given
//As of now the depth is given 4 but can be changed as per requirement
expandNodeTillDepth = function(treePanelId ,nodeId){
var PNode = Ext.getCmp(treePanelId).getNodeById(nodeId);
PNode.collapse(true);
PNode.expand(false);
PNode.eachChild(
myfunction
);
}

//Example Use -
//expandNodeTillDepth('myTree','ynode-18',4)```
http://mishranam.blogspot.com/2011/1...-depth-in.html

2. ## Get All child nodes (till max possible deep level) under a node

Code:
```/**
* 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());```
also look at -
http://mishranam.blogspot.com/2012/0...ng-at-all.html