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