PDA

View Full Version : TreeStore total number of nodes



austin1030
31 May 2012, 10:05 AM
Hi

Does anyone know how to find total number of node on TreeStore when it retrieve data from the server side?

I like to fire an event "beforeload" so that I can give some of restriction.

If you can point a post or solution code would be great!

Thanks

vietits
31 May 2012, 5:23 PM
Try <treeStore>.tree.flatten().length.

austin1030
1 Jun 2012, 8:45 PM
Try <treeStore>.tree.flatten().length.

vietits, I don't think this will work. It returns the root constructor in an array.

I tried to retrieve by
<TreeStore>.getRootNode().childNodes or
<TreeStore>.tree.root.childNodes which hope to return childNodes in array where I can use the length of array for my purpose. But it returns an empty array.

I've been looking in firebug where I can find a way to find total number of nodes but not success.

Any idea?

vietits
2 Jun 2012, 12:37 AM
vietits, I don't think this will work. It returns the root constructor in an array.

No. <treestore>.tree.flatten().length returns the total of nodes in the tree.



I tried to retrieve by
<TreeStore>.getRootNode().childNodes or
<TreeStore>.tree.root.childNodes which hope to return childNodes in array where I can use the length of array for my purpose. But it returns an empty array.

<treestore>.getRootNode().childNodes gives you an array of child nodes directly under root node, not all nodes in the tree.

The example below shows the total number of nodes (which is 9) in the tree and number of child nodes under root node (which is 2):


Ext.onReady(function(){
var store = Ext.create('Ext.data.TreeStore', {
fields: ['text'],
proxy: 'memory',
root : {
text: 'Root',
expanded: true,
children: [{
text: 'Node 1',
expanded: true,
children: [{
text: 'Node 1.1',
expanded: true,
children: [{
text: 'Node 1.1',
leaf: true
},{
text: 'Node 1.2',
leaf: true
}]
},{
text: 'Node 1.2',
leaf: true
}]
},{
text: 'Node 2',
expanded: true,
children: [{
text: 'Node 2.1',
leaf: false,
expandable: true,
children: null
},{
text: 'Node 2.2',
leaf: true
}]
}]
}
});
console.log(store.tree.flatten().length, store.getRootNode().childNodes.length);
});