问题
I have a method that returns a list of roles, I want to put those roles in the jstree but I don't know how.
I tried to do the following but I just don't know how to make a valid json for jstree
function createNodeList() {
$('#processRoleTree').jstree({
"json_data": {
"ajax": {
"type": "POST",
"url": "/TreeLoader.aspx?Action=GetProcessRoles",
"dataType": "json",
"data": function (n) { return { id: n.attr ? n.attr("id") : 0} }
}
},
"plugins": ["json_data", "themes", "ui"]
}).bind("select_node.jstree", function (e, data) {
var selectedObj = data.rslt.obj;
alert(selectedObj.attr("id"));
});
}
on the TreeLoader.aspx page load i have:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Action"].Equals("GetProcessRoles"))
{
GetProcessRoles();
}
}
GetProcessRoles is my method that returns a list of ProcessRole object.
回答1:
{
"data" : "node_title",
// omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function
"attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" },
// `state` and `children` are only used for NON-leaf nodes
"state" : "closed", // or "open", defaults to "closed"
"children" : [ /* an array of child nodes objects */ ]
}
if you are using ajax to get json from server make sure you create json structure like this
[
{ "data" : "A node", "children" : [ { "data" : "Only child", "state" : "closed" } ], "state" : "open" },
"Ajax node"
]
you can see this for details
http://www.jstree.com/documentation/json_data
来源:https://stackoverflow.com/questions/9699749/valid-json-to-create-a-jstree-nodes-dynamically