ExtJS TreePanel - Get All Leaf Nodes and non-leaf nodes of a Tree in ExtJS
***Get All Leaf Nodes and non-leaf nodes of a Tree***
Hi Friends,
After a long time I am writing this small blog post, currently I am really busy working with Alfresco Foundation API, Alfresco is an Open source CMS and once I get time I have a long pipeline of topics in Alfresco programming to post about.
I already have two blog-posts about ExtJS Treepanel, i.e. getting total number of deep-children and getting the array of those all deep-children nodes, this blog-post will be incremental to those previous ones.
Sometimes we might need list of nodes of a Tree with some-special condition may be only leaf-nodes, only non-leaf nodes or may be only nodes with some special attribute, so here I have written two functions which can be modified to get your type of list of nodes.
1) Get the list of all leaf nodes (Only leaf nodes - End-nodes) in a Treepanel in ExtJS
getDeepAllLeafNodes = function(node,onlyLeaf){
var allNodes = new Array();
if(!Ext.value(node,false)){
return [];
}
if(node.isLeaf()){
return node;
}else{
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 leaf nodes Array then;
var allChildNodes = getDeepAllLeafNodes(Node);
//OR to get all leaf child nodes in a Tree
var allChildNodes = getDeepAllLeafNodes(Ext.getCmp(treePanelId).getNodeById(nodeId));
//OR using Root node of a tree- to get all only leaf nodes
var allChildNodes = getDeepAllLeafNodes(Ext.getCmp(treePanelId).getRootNode());
2) Get the list of all only non-leaf-nodes in a Treepanel in ExtJS, (leaf node should not be included)
getDeepAllNonLeafNodes = function(node){
var allNodes = new Array();
if(!Ext.value(node,false)){
return [];
}
if(node.isLeaf()){
return [];
}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 non-leaf deep child nodes Array then;
var allChildNodes = getDeepAllNonLeafNodes(Node);
//OR to get all only non-leaf deep child nodes in a Tree
var allChildNodes = getDeepAllNonLeafNodes(Ext.getCmp(treePanelId).getNodeById(nodeId));
//OR using Root node of a tree- to get all only non-leaf deep child nodes
var allChildNodes = getDeepAllNonLeafNodes(Ext.getCmp(treePanelId).getRootNode());
Comments
Post a Comment