PDA

View Full Version : Expand TreeNode till certain Depth



pramodkhare
11 Dec 2011, 11:58 AM
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


//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)

Also look at this article -
http://mishranam.blogspot.com/2011/12/expand-treenode-till-certain-depth-in.html

pramodkhare
16 Jan 2012, 11:33 AM
/**
* 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/01/getting-all-childnodes-including-at-all.html