SoFunction
Updated on 2025-03-08

The implementation code of NodeCheck of treeview under js script control


function OnTreeNodeChecked(id, type) {
//Get the object that triggers the event
var element = ;
//If the object is not a checkbox, it will not be processed
if (!IsCheckBox(element))
return;
//Get checked status
var isChecked = ;
//Get tree object
var tree = TV2_GetTreeById(id);
//Get the relative node of the element (if it is a leaf node, it is an element, otherwise it is its <A> node)
var node = TV2_GetNode(tree, element);
switch (type) {
case "1":
SetNodesUnChecked(tree);
= true;
break;
case "2":
TV2_SetChildNodesCheckStatus(node, isChecked);
break;
case "3":
TV2_SetChildNodesCheckStatus(node, isChecked);
var parent = TV2_GetParentNode(tree, node);
TV2_NodeOnChildNodeCheckedChanged(tree, parent, isChecked);
}
}
//set all nodes checkbox nochecked
function SetNodesUnChecked(TreeNode) {
var inputs = WebForm_GetElementsByTagName(TreeNode, "INPUT");
if (inputs == null || == 0)
return;
for (var i = 0; i < ; i++) {
if (IsCheckBox(inputs[i]))
inputs[i].checked = false;
}
}
//set child nodes checkbox status
function TV2_SetChildNodesCheckStatus(node, isChecked) {
//Return to the div layer where the current node is located
var childNodes = TV2i_GetChildNodesDiv(node);
if (childNodes == null)
return;
var inputs = WebForm_GetElementsByTagName(childNodes, "INPUT");
if (inputs == null || == 0)
return;
for (var i = 0; i < ; i++) {
if (IsCheckBox(inputs[i]))
inputs[i].checked = isChecked;
}
}
//change parent node checkbox status after child node changed
function TV2_NodeOnChildNodeCheckedChanged(tree, node, isChecked) {
if (node == null)
return;
var childNodes = TV2_GetChildNodes(tree, node);
if (childNodes == null || == 0)
return;
var isAllSame = true;
for (var i = 0; i < ; i++) {
var item = childNodes[i];
var value = TV2_NodeGetChecked(item);
if (isChecked != value) {
isAllSame = false;
break;
}
}
var parent = TV2_GetParentNode(tree, node);
if (isAllSame) {
TV2_NodeSetChecked(node, isChecked);
TV2_NodeOnChildNodeCheckedChanged(tree, parent, isChecked);
}
else {
TV2_NodeSetChecked(node, true);
TV2_NodeOnChildNodeCheckedChanged(tree, parent, true);
}
}
//get node relative element(etc. checkbox)
function TV2_GetNode(tree, element) {
var id = (, "");
id = ().replace(, "");
id = + id;
var node = (id);
if (node == null) //leaf node, no "A" node
return element;
return node;
}
//get parent node
function TV2_GetParentNode(tree, node) {
var div = WebForm_GetParentByTagName(node, "DIV");
//The structure of node: <table>information of node</table><div>child nodes</div>
var table = ;
if (table == null)
return null;
return TV2i_GetNodeInElement(tree, table);
}
//get child nodes array
function TV2_GetChildNodes(tree, node) {
if (TV2_NodeIsLeaf(node))
return null;
var children = new Array();
var div = TV2i_GetChildNodesDiv(node);
var index = 0;
for (var i = 0; i < ; i++) {
var element = [i];
if ( != "TABLE")
continue;
var child = TV2i_GetNodeInElement(tree, element);
if (child != null)
children[index++] = child;
}
return children;
}
function TV2_NodeIsLeaf(node) {
return !( == "A"); //Todo
}
function TV2_NodeGetChecked(node) {
var checkbox = TV2i_NodeGetCheckBox(node);
return ;
}
function TV2_NodeSetChecked(node, isChecked) {
var checkbox = TV2i_NodeGetCheckBox(node);
if (checkbox != null)
= isChecked;
}
function IsCheckBox(element) {
if (element == null)
return false;
return ( == "INPUT" && () == "checkbox");
}
//get tree
function TV2_GetTreeById(id) {
return (id);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//private mothods, with TV2i_ prefix
//////////////////////////////////////////////////////////////////////////////////////////////
//get div contains child nodes
function TV2i_GetChildNodesDiv(node) {
//If == "A", it will not be processed
if (TV2_NodeIsLeaf(node))
return null;
var childNodsDivId = + "Nodes";
return (childNodsDivId);
}
//find node in element
function TV2i_GetNodeInElement(tree, element) {
var node = TV2i_GetNodeInElementA(tree, element);
if (node == null) {
node = TV2i_GetNodeInElementInput(tree, element);
}
return node;
}
//find "A" node
function TV2i_GetNodeInElementA(tree, element) {
var as = WebForm_GetElementsByTagName(element, "A");
if (as == null || == 0)
return null;
var regexp = new RegExp("^" + + "n\\d+$");
for (var i = 0; i < ; i++) {
if (as[i].(regexp)) {
return as[i];
}
}
return null;
}
//find "INPUT" node
function TV2i_GetNodeInElementInput(tree, element) {
var as = WebForm_GetElementsByTagName(element, "INPUT");
if (as == null || == 0)
return null;
var regexp = new RegExp("^" + + "n\\d+");
for (var i = 0; i < ; i++) {
if (as[i].(regexp)) {
return as[i];
}
}
return null;
}
//get checkbox of node
function TV2i_NodeGetCheckBox(node) {
if (IsCheckBox(node))
return node;
var id = + "CheckBox";
return (id);
}