-
10 Dec 2012 12:03 PM #1
Answered: dynamic root nodes and static children
Answered: dynamic root nodes and static children
Hi,
I'm new to Sencha ExtJS, but I'm trying to write an application in it.
Unfortunately, I can't figure out, how to create a treestore, where the root nodes will be loaded dynamically and then static children leafs will be added.
This is the json code, I receive from the source server:
My first problem is, that I don't know how to get the name value for each company, because I don't know what to set as the root node, it doesn't work with 'companies', but neither with 'company'.Code:{ "companies": { "company": [{ "autostart": "false", "createDt": "2012-09-10T13:19:22.645+02:00", "dbName": "demo", "id": "1", "idCsGroup": "39fa897328d1e6a55ab199f1410015d2", "name": "Moje Firma s.r.o.", "show": "true", "status": "ESTABLISHED" }, { "autostart": "false", "createDt": "2012-09-10T13:24:49.765+02:00", "dbName": "demo_de", "id": "1", "idCsGroup": "39fa897328d1e6a55ab199f1410015d2", "name": "Já Ĺ˝ivnostnĂk", "show": "true", "status": "ESTABLISHED" }, { "autostart": "false", "createDt": "2012-09-10T13:35:22.408+02:00", "dbName": "demo_neziskova", "id": "1", "idCsGroup": "39fa897328d1e6a55ab199f1410015d2", "name": "Nezisková organizace", "show": "true", "status": "ESTABLISHED" }, { "autostart": "false", "createDt": "2012-09-10T13:29:10.062+02:00", "dbName": "demo_prispevkovka", "id": "1", "idCsGroup": "39fa897328d1e6a55ab199f1410015d2", "name": "PĹ™ĂspÄ›vková organizace", "show": "true", "status": "ESTABLISHED" }, { "autostart": "false", "createDt": "2011-08-21T15:39:47.604+02:00", "dbGroup": "demo_sk", "id": "1", "idCsGroup": "39fa897328d1e6a55ab199f1410015d2", "name": "Moje Firma SK s.r.o.", "show": "true", "status": "ESTABLISHED" }] } }
And my second problem is how to assign static children to the dynamically loaded nodes.
For each of these companies, I need to assign 5 static children, so the final tree will look similar to this:Is it even possible?Code:-Moje Firma s.r.o. --child1 --child2 --child3 --child4 --child5 -Já Ĺ˝ivnostnĂk --child1 --child2 --child3 --child4 --child5 -Nezisková organizace --child1 --child2 --child3 --child4 --child5 -PĹ™ĂspÄ›vková organizace --child1 --child2 --child3 --child4 --child5 -Moje Firma SK s.r.o. --child1 --child2 --child3 --child4 --child5
Thanks for any reply
-
Best Answer Posted by flanders
Well in that case I would use a static root when instantiating the store. After the store is instantiated you can do a Ext.Ajax.request({}) to the source by hand and use the result to build the tree.
Un-tested dry code:
Not very elegant imho, but it should workCode:Ext.Ajax.request({ url: 'your/source', success: function(response, options) { var json = Ext.decode(response.responseText); Ext.Array.each(json.companies.company, function(item) { var companyNode = yourGrid.getRootNode().appendChild({ text: item.name }); companyNode.appendChild({ text: 'First static item' }); .... }); } });
-
10 Dec 2012 2:09 PM #2Sencha User
- Join Date
- Dec 2009
- Location
- Enschede, The Netherlands
- Posts
- 327
- Vote Rating
- 11
- Answers
- 16
It is a bit of an unusual use case I guess. What I would do is:
- Do not send a root node on your model/store, just set rootVisible: false on the tree this will generate a request to get the root node
- Respond with a rootnode with whatever name and give the items you want as root nodes. Since the real root (a tree always has one real root) is invisible.
- Listen to the load event, so you know when the records are retrieved and shown. At that point you can add your own static children. Alternativly, you can also add them inline serverside (I would recommend that)
The outer object with a success: true and then a data property is the same as on a non-tree store. After that on the data you give an array with nodes. You could already add deeper children using the children property like I do here.Code:{ "success": true, "data": [ { "id": "1", "name": "Home", "templateId": "home.tpl", "active": "1", "created": "2012-10-31 01:28:13", "changed": "2012-10-31 01:28:13", "compositeContentId": "1", "url": "\/", "hasChildren": "1", "children": [ { "id": "2", "name": "nieuwe pagina test", "templateId": "home.tpl", "active": "1", "created": "2012-11-21 14:26:26", "changed": "2012-11-21 14:26:26", "compositeContentId": "134", "url": "\/nieuwepaginatest\/", "hasChildren": "0" } ], "fullIdPathToParent": "", "urlPart": "", "templateName": "Home", "leaf": false } ] }
-
11 Dec 2012 7:35 AM #3
Thanks for reply.
Anyway, I can't change the server respond, because I'm loading the data from a remote server.
-
11 Dec 2012 7:43 AM #4Sencha User
- Join Date
- Dec 2009
- Location
- Enschede, The Netherlands
- Posts
- 327
- Vote Rating
- 11
- Answers
- 16
Well in that case I would use a static root when instantiating the store. After the store is instantiated you can do a Ext.Ajax.request({}) to the source by hand and use the result to build the tree.
Un-tested dry code:
Not very elegant imho, but it should workCode:Ext.Ajax.request({ url: 'your/source', success: function(response, options) { var json = Ext.decode(response.responseText); Ext.Array.each(json.companies.company, function(item) { var companyNode = yourGrid.getRootNode().appendChild({ text: item.name }); companyNode.appendChild({ text: 'First static item' }); .... }); } });
-
11 Dec 2012 7:47 AM #5
Thanks a lot for your replies the append option is right, what I was looking for.


Reply With Quote