Currently, I have two Stored Procedures in SQL Server that handle retrieving a tree from the database:
The first retrieves all of the nodes at a specific level when you pass in a level number.
The other retrieves the children of a specific node when you pass in Level, Left, and Right values.
I’m using MVC 3.
Ideally, I would like to setup JSTree to make a call to the database every time a user clicks to expand a node. So instead of loading the entire tree up in a Json model on the server and then passing it to JSTree as is typical, I’d like to instead ONLY pass in the children for the specific node that is clicked by the user. This will happen at every node so that ONLY the children of the immediate node will have to be passed into the JSTree instead of the entire tree.
Is this possible? And if so, I would appreciate some example code for the view (especially) and possibly the controller using Microsoft’s MVC 3 framework. I’d appreciate any help.
Yes this is possible, it’s actually pretty straightforward with jstree.
What you want to do is use the
ajaxparameter of thejson_datajstree plugin, but set it up so that either thedataorurlparameter is passed a function that will send off which node id has been expanded so that your webservice can make the call to your stored procedure and return the data for the children of the selected node.Here’s one of the examples from http://www.jstree.com/documentation/json_data modified a bit:
You can make this code quite a bit more elegant but that’s the gist. This will allow you to build out the tree on demand in chunks rather than all at once.
If your webservice is set up so that parameters are passed on the url, just make the url the function instead and use that to build up your url request with the id of the node instead or whatever other parameters you need.