The root node is selected by default when "Jstree" jstree-is initialized

Source: Internet
Author: User

varContacttree = $ (' #contact-org '). Jstree ({"Core" : {          "Animation":false,          "Multiple":false,          "Check_callback":true,          "Themes" : {              "Icons":true,              "Dots":false          },          "Data" : {              "DataType": ' JSON ',              "url":function(node) {returnnode.id=== "#"?Ctxpath+ "/v-contact?queryorg": Ctxpath + "/v-contact?queryorg"; },              "Data":function(node) {return{"id": Node.id}; }}). On (' Loaded.jstree ',function(E, data) {varInst =data.instance; varobj =Inst.get_node (E.target.firstchild.firstchild.lastchild);  Inst.select_node (obj);    }); 
View Code

By loaded.jstree This action, when the tree is loaded, it gets the root node object;

Call the Select_node method and select the root node.

Advanced:

Assign the Get_node description (you can pass in the ID to get the node, but in this case, you have to know which is the root node):

Get_node ();

$ ("#demo")

. Bind ("Loaded.jstree", function (E, data) {

Data.inst.open_all (-1); -1 opens all nodes in the container

})

. Jstree ({

Settings for your Jstree

})

Public Object Menu (String Roleid, HttpServletRequest req) {List<SysMenu> list = Menuservice.query (Cnd.orderby (). ASC ("Location"). ASC ("Path")); List<SysMenu> datas =Roleservice.getdatas (); List<NutMap> menus =NewArraylist<>(); List<SysMenu> Rolemenus = roleservice.getmenusandbuttons (Roleid);//permissions for the current role         for(Sysmenu menu:list) {nutmap map=NewNutmap ();  for(Sysmenu bt:datas) {if(Menu.getpath (). Equals (Bt.getpath (). substring (0, Bt.getpath (). Length ()-4)) {Menu.sethaschildren (true);  Break; }            }             for(Sysmenu rm:rolemenus) {if(Menu.getid (). Equals (Rm.getid ())) {Map.put ("State",NewState (false,false,true));//whether to allow editing-whether to expand-whether the tree node is selected                     Break; }} map.put ("id", Menu.getid ()); Map.put ("Text", Menu.getname ()); Map.put ("Icon", Strings.sblank (Menu.geticon ())); Map.put ("Parent", "". Equals (Strings.snull (Menu.getparentid ()))? " #": Menu.getparentid ()); Map.put ("Data", Menu.gethref ());        Menus.add (map); } req.setattribute ("Menus", Json.tojson (menus)); returnStrings.isblank (Roleid)?NULL: Roleservice.fetch (Roleid); } public class State {privateBooleanDisabled; PrivateBooleanopened; PrivateBooleanselected; /** * tree node tick * @param disabled edit * @param opened whether to expand * @param selected Check*/Public State (BooleanDisabledBooleanOpened,Booleanselected) {         This. disabled=Disabled;  This. opened=opened;  This. selected=selected; } PublicBooleanisdisabled () {returnDisabled; } PublicvoidSetdisabled (Booleandisabled) {         This. Disabled =Disabled; } PublicBooleanisopened () {returnopened; } PublicvoidSetopened (Booleanopened) {         This. opened =opened; } PublicBooleanisSelected () {returnselected; } PublicvoidSetSelected (Booleanselected) {         This. selected =selected; }} varJstreemenu; $(function() {Jstreemenu= $ ("#jsTreeMenu"). Jstree ({plugins: ["Wholerow", "checkbox"], Core: {data: ${menus!}}, CheckBox: {three_state:false, Cascade:' Down '}}). On ("Hover_node.jstree",function(E, data) {if(Data.node.data) $ ("#menuDescript"). Text (Data.node.data); }). On ("Dehover_node.jstree",function(E, data) {$ ("#menuDescript"). HTML ("<div class= ' alert alert-info ' > Please tick each menu that needs to be assigned </div>");//Clear Menu Description}). bind ("Loaded.jstree",function(E, data) {//immediately load all nodes under the tree    });    }); functionEach (node, tree, nodes) {varp =tree.get_parent (node); if(P.length > 1) {            if(Nodes&&p&&nodes.indexof (P) <0) Nodes.push (p);        Each (P, tree, nodes); }    }    functionComplete () {varTree = $.jstree.reference ("#jsTreeMenu"); varids =tree.get_selected (); //queries all parent nodes of the selected node         for(vari = 0; i < ids.length; i++) {            varnode =Ids[i];        Each (node, tree, IDS); }        if(Ids.length > 0) {$.post ("url", {menuIds:ids.toString (), Roleid: ' ${obj.id} '},function(data) {if(Data.code = = 0) {toast.success (data.msg); SetTimeout (function () {                    $("#goback"). Trigger ("click")); }, 1000); } Else{toast.error (data.msg); }        }, "JSON"); } Else{toast.warning ("Please select the menu first!" "); }    }</script><%}%>
View Code
$(function() {$.ajaxsetup ({cache:false}); $("#tree"). Tree ({data: {type:"JSON", Async:true, opts: {async:true, Method:"GET",                   //send the requested URL or JSON-formatted data file to the backgroundURL: "Json_data.json"}}, Root: {attributes: {"id": "0"}, Data: "ROOT", State: "Closed"}, lang:{loading:"Directory Loading ..."}, ui:{theme_name:' checkbox '}, callback: {//Make sure static are not used once the tree have loaded for the first timeOnLoad:function(t) {t.settings.data.opts.static=false; }, Beforedata:function(n, t) {if(n = =false) t.settings.data.opts.static=T.settings.root; Else                       return{parent_id: $ (N). attr ("Id") }               },               //When the checkbox tree expands, the settings node is selectedOnopen_all:function() {                 varSelectedids = [];  for(vari = 0; i < selectedids.length; i++) {JQuery.tree.plugins.checkbox.check ($ ("#" + selectedids[i]+ ""));   }}}, plugins:{checkbox:{}});  }); 
View Code

When the tree return data, the data is processed first, and the open and check are appended

Alternative format of the node (ID & Parent is required) {ID:"string" //Required Parent: "string"  //Required text: "string"/ /Node text icon: " String " //String for custom state: {opened: Boolean //are the node open disabled: Boolean //IS The node disabled selected: boolean //Is the node selected}, Li_attr: {} //attributes for the Generat Ed LI node a_attr: {} //Attributes for the generated a node}

The root node is selected by default when "Jstree" jstree-is initialized

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.