PDA

View Full Version : Find tree node by node id



dekely
14 Oct 2013, 5:30 AM
Hi All,
I searched a lot and still can't find the right way.
How do I find a node by id? does it work recursively?

This is my node:
{id: tabConfig.itemId, text: tabConfig.title, leaf: true }

The only way I found is this:


this.getRootNode().childNodes.each(function(child){
if(child.raw.itemId == id){
child.destroy();
return;
}
});

ettavolt
14 Oct 2013, 5:33 AM
You can try treeStore.getNodeById (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.TreeStore-method-getNodeById).

dekely
14 Oct 2013, 5:50 AM
I think I already tried that. Maybe the problem is with my store.
How can I see the records in the store?

Arg0n
14 Oct 2013, 6:24 AM
findChild (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.NodeInterface-method-findChild)



var node = store.getRootNode().findChild('itemId', 123, true); //true means deep (recursive)


Should work :x


Seeing all records in a tree store (eachChild (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.NodeInterface-method-eachChild)):


store.getRootNode().eachChild(function(child) {
console.log(child, child.getData()); //Displays the child as object and it's data =)
});


And don't use the "raw" values if you don't know what you are doing. Raw values containing the raw data, which was loaded into the record. So if you have some mapping or some convert functions, it will not display your mapped or converted data. It would display the data before it was mapped or converted. I hope you understand what I mean xD

dekely
14 Oct 2013, 6:47 AM
Still doesn't work, here is a basic example:




Ext.onReady(function () {
var store = Ext.create('Ext.data.TreeStore', {
root: {}
});


Ext.create('Ext.tree.Panel', {
id: 'tree',
title: 'Simple Tree',
width: 200,
height: 350,
store: store,
rootVisible: false,
renderTo: Ext.getBody()
});


Ext.create('Ext.Button', {
text: 'Add Node To Root',
renderTo: Ext.getBody(),
handler: function() {
var newChildNode = { itemId: 11, text: 'new node', expanded: true, leaf: true };
var newNode = { itemId: 1, text: 'new node', expanded: true, children:[newChildNode] };
newNode = Ext.getCmp('tree').getRootNode().appendChild(newNode);

var tree = Ext.getCmp('tree');
//debugger;
tree.getView().select(newNode.firstChild);
}
});


Ext.create('Ext.Button', {
text: 'Find Node',
renderTo: Ext.getBody(),
handler: function() {
var node = Ext.getCmp('tree').store.getRootNode().findChild('itemId', 1, true); //true means deep (recursive)
alert(node);
}
});


});

Arg0n
14 Oct 2013, 8:30 AM
Ha!! Got it! xD

Your store or better your tree store model doesn't know the field "itemId". So if you want to write something with an itemId in it, it will ignore it. So you have to add this field to your store or the better way: creating a model for your tree store.



var store = Ext.create('Ext.data.TreeStore', {
root: {},
fields: [{
name: 'itemId',
type: 'int'
}, {
name: 'text',
type: 'string'
}]
});


Here a fiddle example with just adding the fields:

https://fiddle.sencha.com/#fiddle/vd

dekely
14 Oct 2013, 8:54 AM
God bless you !!
Thanks!!