Bootstrap-treeview enables full and reverse selection, and selects all sub-item parent classes by default,

Source: Internet
Author: User

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

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.