Simple implementation of the dojo tree with checkbox and cascading Selection

Source: Internet
Author: User

The tree that comes with dojo cannot implement the checkbox node selection function. The third-party cbtree is too complex. Therefore, you have written a simple implementation to implement child node cascade parent nodes until the root node, select all child nodes for the parent node, but the child node is not canceled when the parent node is canceled. The Code is as follows. You can copy and modify it yourself.

Function createmenutree (){
Require (["dojo/request ",
"Dojo/store/memory ",
"Dijit/tree/objectstoremodel ",
"Dijit/tree"], function (request, memory, objectstoremodel, tree ){
Request ("/role/menus/", {handleas: "JSON", preventcache: true}). Then (function (data1 ){

For (VAR I = 0; I <data1.length; I ++ ){
Data1 [I]. Checked = false;
}

Menustore = new memory ({
Idproperty: "PID ",
Data: data1,
Getchildren: function (object ){
Return this. Query ({pparent: object. PID });
}
});

Menumodel = new objectstoremodel ({
Store: menustore,
Labelattr: "pname ",
Query: {pid: "menu "},
Mayhavechildren: function (item ){
VaR subs = menustore. Query ({"pparent": item. PID });
Return subs. length> 0;
}
});

Menutree = new tree ({
Model: menumodel,
Autoexpand: True,
Onclick: function (item, node, EVT ){
Console. debug (menutree. paths );

VaR selcb = dojo. byid ("pjtchk" + item. PID );
If (selcb = NULL) {return ;}
If (! Dojo. hasattr (selcb, "checked ")){
Node. labelnode. innerhtml = "<input id = 'pjtchk" + item. PID + "'checked type = 'checkbox'/>" + item. pname;
Selectuppers (item. PID );
Selectsubs (item. PID );
} Else {node. labelnode. innerhtml = "<input id = 'pjtchk" + item. PID + "'Type = 'checkbox'/>" + item. pname ;}
},
_ Createtreenode: function (/* object */args ){
VaR tnode = new dijit. _ treenode (ARGs );
Tnode. labelnode. innerhtml = "<input id = 'pjtchk" + args. item. PID + "'Type = 'checkbox'/>" + args. label;
Return tnode;
}
});

Menutree. placeat ($ ("menutree "));
Menutree. startup ();
});
});
}

 

Function selectuppers (ID ){
VaR smenus = menustore. Query ({pid: Id });
If (smenus! = NULL & smenus. length> 0 ){
VaR cb = $ ("pjtchk" + id );
If (CB! = NULL ){
Dojo. ATTR (CB, 'checked', true );
Selectuppers (smenus [0]. pparent );
}
}
}

Function selectsubs (ID ){
VaR smenus = menustore. Query ({pid: Id });
If (smenus! = NULL & smenus. length> 0 ){
VaR cb = $ ("pjtchk" + id );
If (CB! = NULL ){
Dojo. ATTR (CB, 'checked', true );
}
}

VaR menus = menustore. Query ({pparent: Id });
If (menus! = NULL & menus. length> 0 ){
Dojo. foreach (menus, function (menu ){
Selectsubs (menu. PID );
});
}
}

Simple implementation of the dojo tree with checkbox and cascading Selection

Related Article

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.