How to store Multiple Pieces of Data in jsTree nodes using a Single Attribute?
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 v开发者_如何转开发alue. Is there a better way to do this?
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!
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)
);
精彩评论