Adding metadata to jsTree

I just can't figure it out or find any documents.

I have a JSON head:

{"data": "node", "metadata": {"content": "hellooooo"}}

This loads, but I can’t figure out how to write to this field, retrieve this field and guarantee that it will be created when creating a new node.

Where are the documents for metadata located?

Thanks, Marco.

+6
json metadata jstree
source share
2 answers

I found the answer at http://groups.google.com/group/jstree/browse_thread/thread/28d0c8d3eb2d9f8f

if you use JSON and you deliver your nodes with such metadata:

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

... you can get (and install) such data:

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

This works with the latest commit (RC3 / r233). In older versions it uses

 $(data.rslt.obj).data("jstree").description 

The latter solution worked for me (the default download is rc2).

+7
source share

Thank you, I’ve lost my mind. None of the old examples work! So, I finally can access the metadata, the problem is that I do not know how to iterate over an unknown number of metadata fields?

ok, now I checked it and you can iterate over the object returned by data () without named parameters

 .bind("select_node.jstree", function (e, data) { var propsObj = $(data.rslt.obj).data(); for (var prop in propsObj) { alert(prop + " = " + propsObj[prop] + "\n"); } }); 

If you need to avoid creating the jstree_children array, the best way, in my opinion, is to encapsulate metadata in another object:

 "metadata" : {"properties" : {"prop1" : "aa1a", "prop2" : "123"}} 

you can iterate with:

 var metadata = $(data.rslt.obj).data(); for (var prop in metadata.properties) {...} 
+1
source share

All Articles