metadata in json_data plugin

1,154 views
Skip to first unread message

Jake Verbaten

unread,
Jan 17, 2011, 12:01:47 PM1/17/11
to jsTree
As by the documentation you can set metadata on the `<li>` node when
passing in json data.

The documentation either doesn't mention how to get at the metadata I
set or I can't get at it all.

Having tracked down what it does with my meta data in the source of
v1.0rc2 I think there is a bug or I have a misunderstanding

`if(js.metadata) { d.data(js.metadata); }`

at line 1765 in my copy. seems to use a getter for .data rather then a
setter so the metadata appears to just be lost.

Is this a bug or is there some other way I can access my meta data? I
can defiantly get access by changing that line to

`if(js.metadata) { d.data("jstree-metadata", js.metadata);` and
calling `$("#id").data("jstree-metadata")`

If this is already fixed is there a version later then v1.0rc2 ?

Markus Vetter

unread,
Jan 18, 2011, 2:34:07 AM1/18/11
to jst...@googlegroups.com
Hello Jake,

if you're using JSON and you deliver your nodes with a metadata like this:

{
"data": "This is the name of the node",
"metadata": {
"number": "no1",
"description": "Description"
}
}

...you'll be able to get (and set) the data like this:

$('div#jstree')
.jstree(
...options...
)
.bind('select_node.jstree', function(e, data){
alert( $(data.rslt.obj).data('description') );
});

This works with the newest commit (rc3/r233). In older versions it uses
$(data.rslt.obj).data("jstree").description
...hope this helps.

Grettings,
Markus.

Jake Verbaten

unread,
Jan 18, 2011, 4:51:47 AM1/18/11
to jsTree
I didn't think of setting it as an object. If it's not possible to set
it as a string then that example should be removed from the
documentation. (on the json_data page).

mvwd

unread,
Jan 18, 2011, 7:03:36 AM1/18/11
to jsTree
...maybe you misunderstood this sentence in docu:
"// the `metadata` property will be saved using the jQuery `data`
function on the `li` node
metadata : "a string, array, object, etc","
This means: you can fill the (meta-)data-Object with a string, array
or an object.
How to use jQuery's data() => http://api.jquery.com/data/

Markus.
Reply all
Reply to author
Forward
0 new messages