Loading Data into a Tree

34 views
Skip to first unread message

Michel

unread,
Mar 27, 2008, 12:08:49 PM3/27/08
to GWT-Ext Developer Forum
Hi Everyone.
Image I have a String with json.

I want to use that string to load data into a TreePanel.

Is it possible?

1 - How do I do that, I mean, which method in TreePanel (or
TreePanelLoader) accepts a String?

2 - How should be the Jason structure?

Thanks for helping,
Michel.

leo_BsAs

unread,
Mar 27, 2008, 2:04:44 PM3/27/08
to GWT-Ext Developer Forum
Hi Michel...

The string should be in the follow format:
[{ 'id': 1, 'text': 'A folder Node', 'leaf': false, childNodes:
[{ 'id': 2, 'text': 'A leaf Node', 'leaf': true },{ 'id': 2, 'text':
'A leaf Node', 'leaf': true }] },
]

I implemented a servlet response that brings me a json string in the
above form. Then I point the loader to the url:

final TreeLoader loader = new TreeLoader();
loader.setDataUrl("consultarUsuarios.json");

It works fine for me.

Regards,
Leo

leo_BsAs

unread,
Mar 27, 2008, 2:20:28 PM3/27/08
to GWT-Ext Developer Forum
Sorry, it is not childNodes, it is children...
This is an example of the response that I get from the server:
[{"children":[],"id":"10177","leaf":true,"text":"Argentina"},
{"children":[],"id":"10108","leaf":true,"text":"Uruguay"}]

If you want you can try a different way... like this:

public void expandNode(JSONArray array) {
for (int i = 0; i < array.size(); i++) {
String item =
((JSONString)array.get(i).isObject().get("description")).stringValue();
getSelectedNode().appendChild(new TreeNode(item));
}
getSelectedNode().expand();
}


I hope it helps...
Leo

Michel

unread,
Mar 28, 2008, 5:01:53 AM3/28/08
to GWT-Ext Developer Forum
I din't want to do this using a servlet...
The problem is that my server service is going to return me the json
(String, not file)...
And then I would have the dummy overhead of creating a servlet to
create a file from that String... which simply doesn't make sense for
me....

Isn't there any way of using the Json in String, as it is now??

Your second exemple,
public void expandNode(JSONArray array) {
for (int i = 0; i < array.size(); i++) {
String item =
((JSONString)array.get(i).isObject().get("description")).stringValue();
getSelectedNode().appendChild(new TreeNode(item));
}
getSelectedNode().expand();
}

I didn't understand.... what is that expandNode method? and that
getSelectedNode method??
Are you using gwt-ext 2.0.2?
Those methods are not available in the TreePanel class...

Thanks anyway... hopes anyone can still give one possible way of
loading the TreePanel (using a Loader or not) using Json in String
format (not passing any URL).

Best regards,
Michel.

leo_BsAs

unread,
Mar 28, 2008, 10:02:16 AM3/28/08
to GWT-Ext Developer Forum
Michel,
the expandNode method is an example of what you can implement by your
own into your code to load a tree with a JSON string... You can
replace the getSelectedNode by a node in the following way (I'll
rewrite the method):

public void nodeLoader(TreeNode node, JSONArray array) {
for (int i = 0; i < array.size(); i++) {
String item =
((JSONString)array.get(i).isObject().get("description")).stringValue();
node.appendChild(new TreeNode(item));
}
node.expand();
}

Two considerations:
* You have to remember to set a root node to the tree panel:
TreeNode root = new TreeNode();
treePanel.setRootNode(root);
and append nodes to it (eg. nodeLoader(root, jsonArray); )
* With this method you're free of handle the json structure that you
want. If you have children in your json structure, you can use the
method above in recursion.

I hope to be clear, I'm not fluent in English... sorry :(

Regards,

Leo

Michel

unread,
Mar 31, 2008, 7:57:46 AM3/31/08
to GWT-Ext Developer Forum
Hello Leo.

Thanks for your reply.
I will give it try, so that I could add the nodes manually.

But the better aproach will be if Sanjiv could create a new method,
accepting the XML/Json in String format, and not only the ones that
accept an URL :-)

Best regards,
Michel.

Sanjiv Jivan

unread,
Mar 31, 2008, 8:07:48 AM3/31/08
to gwt...@googlegroups.com
Please create an issue in tracker : http://code.google.com/p/gwt-ext/issues/list

Sanjiv

Michel

unread,
Mar 31, 2008, 8:23:32 AM3/31/08
to GWT-Ext Developer Forum
Done: http://code.google.com/p/gwt-ext/issues/detail?id=294

Thanks,
Michel.

On 31 mar, 14:07, "Sanjiv Jivan" <sanjiv.ji...@gmail.com> wrote:
> Please create an issue in tracker :http://code.google.com/p/gwt-ext/issues/list
>
> Sanjiv
>
Reply all
Reply to author
Forward
0 new messages