Bootstrap-treeview enables full and reverse selection, and selects all sub-item parent classes by default,
// Parameters in the selection and unselected methods of the treeview
OnNodeChecked: function (event, node ){
Var selectNodes = getChildNodeIdArr (node); // obtain all child nodes
If (selectNodes) {// if the subnode is not empty, all subnodes are selected.
$ ('# Using_json'). treeview ('checknode', [selectNodes, {silent: true}]);
}
Var parentNode = $ ("# using_json"). treeview ("getNode", node. parentId );
SetParentNodeCheck (node );
},
OnNodeUnchecked: function (event, node ){
// Checkmenus. removevalue (node );
// Cancel parent node and child node Cancellation
Var selectNodes = setChildNodeUncheck (node); // obtain unselected subnodes
Var childNodes = getChildNodeIdArr (node); // obtain all child nodes
If (selectNodes & selectNodes. length = 0) {// if the number of subnodes with subnodes not selected is 0, all subnodes are deselected.
Console. log ("invert ");
$ ('# Using_json'). treeview ('uncheckknode', [childNodes, {silent: true}]);
}
// Cancel the node parent node
Var parentNode = $ ("# using_json"). treeview ("getNode", node. parentId); // obtain the parent node
Var selectNodes = getChildNodeIdArr (node );
SetParentNodeCheck (node );
}
// Select all child nodes when selecting the parent node
Function getChildNodeIdArr (node ){
Var ts = [];
If (node. nodes ){
For (var x in node. nodes ){
Ts. push (node. nodes [x]. nodeId );
If (node. nodes [x]. nodes ){
Var getNodeDieDai = getChildNodeIdArr (node. nodes [x]);
For (var j in getNodeDieDai ){
Ts. push (getNodeDieDai [j]);
}
}
}
} Else {
Ts. push (node. nodeId );
}
Return ts;
}
// When all child nodes are selected, the parent node is canceled when the parent node is selected.
Function setParentNodeCheck (node ){
Var parentNode = $ ("# using_json"). treeview ("getNode", node. parentId );
If (parentNode. nodes ){
Var checkedCount = 0;
For (var x in parentNode. nodes ){
If (parentNode. nodes [x]. state. checked ){
CheckedCount ++;
} Else {
Break;
}
}
If (checkedCount = parentNode. nodes. length) {// if all the child nodes are selected, select all the parent nodes.
$ ("# Using_json"). treeview ("checkNode", parentNode. nodeId );
SetParentNodeCheck (parentNode );
} Else {// if not all child nodes are selected, the parent node is not selected.
$ ('# Using_json'). treeview ('uncheckknode', parentNode. nodeId );
SetParentNodeCheck (parentNode );
}
}
}
// Cancel all child nodes when canceling the parent node
Function setChildNodeUncheck (node ){
If (node. nodes ){
Var ts = []; // The unselected set of the current node subset
For (var x in node. nodes ){
If (! Node. nodes [x]. state. checked ){
Ts. push (node. nodes [x]. nodeId );
}
If (node. nodes [x]. nodes ){
Var getNodeDieDai = node. nodes [x];
For (var j in getNodeDieDai ){
/**
* I wrote in the original reprinted article! GetNodeDieDai. nodes [x]. state. checked
* However, the test is unavailable and the. nodes [x] is removed.
*/
If (! GetNodeDieDai. state. checked ){
Ts. push (getNodeDieDai [j]);
}
}
}
}
}
Return ts;
}
Main content of this article