Loading jsTree Children from JSON
I'm fairly new to JavaScript coding, specifically on using jQuery. I'm trying to use jsTree trying to populate it through JSON. Now I believe my question had been answered on related questions but I can't seem to find a specific answer to what I'm doing. I have referred to the jsTree documentation as well, but I still can't figure it out.
below is the code I'm trying out
buildTree : function(name, url, nodeRef) {
$("#"+name).jstree({
"json_data" : {
"ajax" : {
"url" : url + nodeRef,
"data" : function(n) {
return { id : n.attr ? n.attr("id") : 0 };
}
}
},
"plugins" : [ "themes", "json_data" ]
});
}
this is basically a copy of the code in the jsTree documentation for JSON. What's happening here is I am able to populate the root folder of the tree of it'开发者_高级运维s contents. Below is the response of the JSON I'm getting.
[
{
"attr":{"id":"a3e6a1f0-ec50-4215-a6df-2065eb09115e","rel":"file"},
"data":"File 1","state":""
},
{
"attr":{"id":"b38e4a72-875b-4d69-95d3-5437f7e65575","rel":"folder"},
"data":"Folder 1","state":"closed"
}
]
But when I click the the node of the folder, It is being populated by the same JSON request, having the same values.
I know the culprit is
"url" : url + nodeRef,
but I am lost on how I'm going to pass the ID I'm getting from the response, replacing the initial nodeRef value. I have tried using the callbacks but nothing is happening.
Any help is very much appreciated.
The following part in your code already takes care of appending an "id" querystring parameter to the ajax url. The function returns an object with key/value pairs that will be used for querystring parameters for the url to be requested.
"data" : function(n) { return { id : n.attr ? n.attr("id") : 0 }; }
I am not sure what the purpose of the nodeRef variable is in your code. Is it to find the root node? If so, could you not use a different querystring parameter for that?
Use fiddler to confirm the urls constructed by jsTree are what you expect it to be. Perhaps it's currently creating a url with two id querystring parameters and it always uses the one provided by your nodeRef variable?
精彩评论