<script type="text/javascript">
//trying to mimmick dee's answer here: http://stackoverflow.com/questions/32290570/lazy-loading-treeview-with-jstree-in-asp-net-mvc
$(function() {
var $children = $("#object-children-tree");
$children.jstree({
"core": {
"animation": 0,
"data": {
"url": function(node) {
return '@Url.Action("GetNodes", "BatchData")';
},
"data": function (selectedNode) {
// Hopefully, each time jstree needs to make an AJAX call this function will be called.
// # is the special ID that the function receives when jstree needs to load the root nodes.
if (selectedNode.id == "#")
return { "selectedNodeId": selectedNode.id }
if (selectedNode.data.nodeType == "provider")
return { "selectedNodeId": selectedNode.id, "selectedNodeType": selectedNode.data.nodeType }
if (selectedNode.data.nodeType == "fileType")
return { "selectedNodeType": selectedNode.data.nodeType, "blockId": selectedNode.data.blockId, "selectedNodeParentId": selectedNode.parent }
return {"selectedNodeId": selectedNode.Id}
}
}
},
"plugins": ["wholerow"]
});
});
</script>
<div id="object-children-tree">
@* Content will be populated by jsTree *@
</div>
I have been reading the jstree documentation and threads on ajax and callback. However, I can't figure out how to apply them to my problem. My tree has five levels. When I toggle a node on level one, I want it to call a particular server action ("/controller/action" b/c I'm using MVC). Then level two children should appear, being created from the results from the server. When I toggle a node on level two, I want it to call a different server action ("/controller/differentAction" - MVC). Then level three children should appear. And so on: toggle a level three node and a different action is called and the response is used to generate level four children. If this has certainly been answered, can you direct me where to look, and possibly explain how it applies? I think the difference with my example is the there is a different action called by toggling a node on each level. I need to do this because the number of children for each node is very great.
Thanks,
**EDIT: ** I have forgone the idea of calling different actions and just allow a particular action to handle which other action to call, which is fine.
[HttpGet]
public ActionResult GetNodes(string selectedNodeId = null, string selectedNodeType = null, string blockId = null, string selectedNodeParentId = null)
{
if (selectedNodeId == "#") //Root nodes
{
return AllProviders();
}
if (selectedNodeType == "provider")
{
return FileTypesOfProvider(Convert.ToInt32(selectedNodeId));
}
if (selectedNodeType == "fileType")
{
return FilesOfProviderOfType(Convert.ToInt32(blockId), Convert.ToInt32(selectedNodeParentId));
}
return AllProviders();
}