Expand Tree/Node Till certain depth in ExtJS


Recently I had a requirement where in our UI screen (which we have built using ExtJS Framework) we had a data-tree-panel and we wanted to expand that tree till a particular depth, and there is no built-in method available in API, so I had written a custom function (I think this will work for all versions of ExtJS).

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 it gets to leaf node.

//This Function recurses through Every Node 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);

Enjoy.

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