Jstree retrieving data from the background via Ajax

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.