PDA

View Full Version : Get TreeStore node by internalId.



incutonez
21 Mar 2012, 4:23 PM
Is it possible to get a node by its internalId? I tried:



Ext.getCmp('myTree').store.getNodeById('ext-record-2'); // where ext-record-2 is the first test node, returns undefined
Ext.getCmp('myTree').getRootNode().findChild('id', 'ext-record-2', true); // returns null
Ext.getCmp('myTree').getRootNode().findChild('internalId', 'ext-record-2', true); // I was just trying things at this point.


I realize this probably isn't possible, so I'll explain what I'm trying to do... I'm trying to change the id of a node, so when I use `getNodeById`, I can use the new id that I made. The idea is, if I changed the id, I could find it in the tree and it would be unique because I only allow for adding unique id's... but the id wouldn't change. I then thought using the 'ext-record' id would make sure I return a unique id, but then I couldn't figure that out, haha.

Here's some code showing what I'd like to do.

Example

var node = Ext.getCmp('myTree').store.getNodeById('test'); // this returns the first test node
node.beginEdit();
node.set('id', 'blah');
node.endEdit();
node.commit(false);
alert(Ext.getCmp('myTree').store.getNodeById('blah')); // returns undefined
alert(Ext.getCmp('myTree').store.getNodeById('test')); // still returns my test node


I tried using the tree's sync method, but that didn't do anything. I'm just wondering how I can update the node's id, so when I use `getNodeById`, I can use the new id. Any thoughts?


**EDIT... here's all of the code that I'm using. When I append a child twice to the store, I now have two records with the same id and text, but if I use the `getNodeById` function, it only returns the first match... so I'd like to search by its internalId (because that's unique).

The Store


Ext.define('myStore', {
extend: 'Ext.data.TreeStore',
storeId: 'treestore',
root: {
text: 'root',
children: [{
text: 'leaf1',
id: 'leaf1',
children: [{
text: 'child1',
id: 'child1',
leaf: true
},{
text: 'child2',
id: 'child2',
leaf: true
}]
},{
text: 'leaf2',
id: 'leaf2',
leaf: true
},{
text: 'leaf3',
id: 'leaf3',
leaf: true
}]
},
proxy: {
type: 'memory',
reader: {
type: 'json'
}
}
});


The Tree

Ext.create('Ext.tree.Panel', {
id: 'myTree',
rootVisible: false,
store: Ext.create('myStore'),
width: 300,
height: 500,
renderTo: Ext.getBody()
});

Then run this command twice



Ext.getCmp('myTree').getRootNode().appendChild({id: 'test', text: 'test', leaf: true}); // this test node takes on the internalId 'ext-record-2'
Ext.getCmp('myTree').getRootNode().appendChild({id: 'test', text: 'test', leaf: true}); // this test node takes on the internalId 'ext-record-3'

dawesi
21 Mar 2012, 6:47 PM
can you explain your use case senario futher.

incutonez
22 Mar 2012, 3:24 AM
Gladly. I'd like to give the user the ability to edit the name of a node (this I can do, and it seems to work just fine), so when they edit the name of the node, I want the ID to reflect that name... so I want to change the ID as well.

The ID does update in the store, but then I come to the problem of "what if they use the same text on another node, thus making the same ID as a node already in the store?" That's why I would like to use the internalId because it seems to be unique to that node's record.

incutonez
22 Mar 2012, 6:44 AM
This was a simple fix... instead of having an ID when I appended the child, I just let Ext take care of it--which assigns the ext-record-# scheme as the ID for the node. Whenever I want to use getNodeById, I just use the internalId, and Ext can grab it just fine.

Only problem is, when I want to use findChild('id', 'ext-record-#', true), I don't get a result, which is odd because getNodeById works just fine. I fixed the findChild problem by using the findChildBy method instead.