PDA

View Full Version : How to load Ext.data.TreeStore from memory



Ex_Soft
28 Feb 2012, 6:38 AM
I've found How To Use ExtJS 4 TreePanels with Both Static Data and Model Stores (http://www.clintharris.net/2011/how-to-use-extjs-4-treepanels-with-both-static-data-and-model-stores/) But in my case:


Ext.define("TestTreeStoreModel1", {
extend: "Ext.data.Model",
fields: [
{ name: "id", type: "int" },
{ name: "parentId", type: "int" },
"caption"
]
});

var
data = {
"rows": [
{ "id": 1, "parentId": null, "caption": "Folder# 1" },
{ "id": 2, "parentId": 1, "caption": "Leaf# 1.1" },
{ "id": 3, "parentId": 1, "caption": "Leaf# 1.2" },
{ "id": 4, "parentId": null, "caption": "Folder# 4" },
{ "id": 5, "parentId": 2, "caption": "Leaf# 2.1" },
{ "id": 6, "parentId": 2, "caption": "Leaf# 2.2" }
]
},
store = Ext.create("Ext.data.TreeStore", {
model: "TestTreeStoreModel1",
proxy: {
type: "memory",
reader: {
type: "json",
root: "rows"
}
},
root: {
id: "rows"
},
listeners: {
append: function(thisNode, newChildNode, index, eOpts) {
if(window.console && console.log)
console.log("Ext.data.TreeStore.append(%o)", arguments);

if(!newChildNode.isRoot())
{
newChildNode.set("text", newChildNode.get("caption"));
}
}
}
}),
t = Ext.create("Ext.tree.Panel", {
store: store,
renderTo: Ext.getBody()
});

store.load(data);

data doesn't load.

mitchellsimoens
28 Feb 2012, 8:30 AM
You should work with the node instead:


store.getRootNode().appendChild([
{ "id": 1, "parentId": null, "caption": "Folder# 1" },
{ "id": 2, "parentId": 1, "caption": "Leaf# 1.1" },
{ "id": 3, "parentId": 1, "caption": "Leaf# 1.2" },
{ "id": 4, "parentId": null, "caption": "Folder# 4" },
{ "id": 5, "parentId": 2, "caption": "Leaf# 2.1" },
{ "id": 6, "parentId": 2, "caption": "Leaf# 2.2" }
]);