Page code:
<id= "Menutree"></div>
JavaScript code:
$ (document). Ready (function($) {initmenutree ();});functionInitmenutree () {$ (' #MenuTree '). Data (' Jstree ',false); $.getjson (' @Url. Action ("Getmenutree", "Adminmenu") ',function(JSON) {$ (' #MenuTree '). Jstree ({' Core ': { ' Data ': JSON}}); }); $(' #MenuTree '). On (' Changed.jstree ',function(node,data) {varid = data.instance.get_node (data.selected[0]). ID;//Node Click eventGet IDClickmenutree (ID); }) $(' #MenuTree '). On (' Loaded.jstree ',function(E, data) {Data.instance.open_all ();//Expand all nodes by default })}
Use jquery's Getjson method to get the data from the background, and then put it directly behind data.
Background code:
Models: (This is the format required by Jstree)
Public classAdminmenutreenotemodel { Public stringID {Get;Set; } Public stringParent {Get;Set; } Public stringText {Get;Set; } Public stringicon {Get;Set; } }
Controllers: (I'm using. Net MVC that's not important, just get the data out in the JSON format as specified)
PublicActionResult Getmenutree () {varTrees = fromAinchDBC. AdminmenusSelect NewAdminmenutreenotemodel {ID=a.id. ToString (), Parent= (A.parentmenu >0? A.parentmenu.tostring ():"#"),//The parent of the default root node is "#"Text =a.title, Icon= (A.iconclass.length >0? A.iconclass:"Icon-doc")//Part of the section set the icon, no icon using the default icon}; returnJson (trees. ToList (), jsonrequestbehavior.allowget);}
It's still very simple. But the documents on the official website of Jstree are not understood at all.
Here is the specified JSON format:
//Alternative format of the node (ID & Parent is required){ID:"string" //RequiredParent:"string" //RequiredText:"string" //node TextIcon:"string" //String for customState : {Opened:boolean//is the node openDisabled:boolean//is the node disabledSelected:boolean//Is the node selected}, Li_attr: {}//attributes for the generated LI nodeA_attr: {}//attributes for the generated A node}
Jstree retrieving data from the background via Ajax