3

In order to store arbitrary data in jsTree nodes, I'm passing a custom attribute (say node-data) to the create_node method. But I need to store more than one piece of data in the single attribute. (For example name,age etc.) What format would be best suited for the value of the node-data attribute? What I'm looking for is a format that will allow easy retrieval of data.

For example we can have a format like "name:John,age:26". But I will have to split the string by using the comma as the delimiter and split at the colon to separate the name and value. Is there a better way to do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Can't Tell
  • 12,714
  • 9
  • 63
  • 91

2 Answers2

2

Any reason you're not using data- attributes? Supported by jquery and every current browser (HTML5). So instead of obj.attr("myattrib", true); You can assign arbitrary data to any object. You could easily do things like:

node.data("name", "John");
node.data("age", 26);

and later you retrieve them when needed:

var name = node.data("name");

on the server end, presuming you're sending your data back as Json data, you simply define (in the simplest form, you can get fancier) a Dictionary metadata in the json object in the server, then you retrieve "name" from the dictionary, cast the object to the appropriate type and away you go!

Chris
  • 154
  • 1
  • 9
  • would you elaborate a little bit more on the idea of preparing the data on the JSON object? I'm trying to send folders permissions when loading a jsTree structure. – Felipe Leão Jul 08 '14 at 14:43
-1

You can pass html attributes to node, using li_attr and a_attr in json data array.

$list[] = array(
                'id' => "year_" . $year->Year,
                'text' => $year->Year,
                'children' => TRUE,
                'folder' => 'folder',
                'li_attr' => array('year' => $year->Year),
                'a_attr' => array('month' => $year->month)
);