用js指令碼控制asp.net下treeview的NodeCheck的實現代碼

來源:互聯網
上載者:User

增加CheckBox聯動的情況選擇:
1.單選TreeView中的任一節點
2.當一節點CheckBox屬性值改變時:子節點的CheckBox屬性值跟隨其改動,父節點不變;
父節點的所有子節點的CheckBox屬性值都為false時才為false;有一個子節點的CheckBox屬性值true時則為true.
3.當一節點CheckBox屬性值改變時:子節點、父節點的CheckBox屬性值跟隨其改動;
父節點的所有子節點的CheckBox屬性值都為false時才為false;有一個子節點的CheckBox屬性值true時則為true.
javascript代碼 複製代碼 代碼如下:function OnTreeNodeChecked(id, type) {
//擷取觸發事件的對象
var element = window.event.srcElement;
//如果對象不是checkbox則不處理
if (!IsCheckBox(element))
return;
//擷取checked狀態
var isChecked = element.checked;
//擷取tree對象
var tree = TV2_GetTreeById(id);
//擷取element的相對結點(如果是葉結點,則就為element,否則為其<A>結點)
var node = TV2_GetNode(tree, element);
switch (type) {
case "1":
SetNodesUnChecked(tree);
element.checked = 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 || inputs.length == 0)
return;
for (var i = 0; i < inputs.length; i++) {
if (IsCheckBox(inputs[i]))
inputs[i].checked = false;
}
}
//set child nodes checkbox status
function TV2_SetChildNodesCheckStatus(node, isChecked) {
//返回當前node所在的div層
var childNodes = TV2i_GetChildNodesDiv(node);
if (childNodes == null)
return;
var inputs = WebForm_GetElementsByTagName(childNodes, "INPUT");
if (inputs == null || inputs.length == 0)
return;
for (var i = 0; i < inputs.length; 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 || childNodes.length == 0)
return;
var isAllSame = true;
for (var i = 0; i < childNodes.length; 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 = element.id.replace(tree.id, "");
id = id.toLowerCase().replace(element.type, "");
id = tree.id + id;
var node = document.getElementById(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 = div.previousSibling;
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 < div.childNodes.length; i++) {
var element = div.childNodes[i];
if (element.tagName != "TABLE")
continue;
var child = TV2i_GetNodeInElement(tree, element);
if (child != null)
children[index++] = child;
}
return children;
}
function TV2_NodeIsLeaf(node) {
return !(node.tagName == "A"); //Todo
}
function TV2_NodeGetChecked(node) {
var checkbox = TV2i_NodeGetCheckBox(node);
return checkbox.checked;
}
function TV2_NodeSetChecked(node, isChecked) {
var checkbox = TV2i_NodeGetCheckBox(node);
if (checkbox != null)
checkbox.checked = isChecked;
}
function IsCheckBox(element) {
if (element == null)
return false;
return (element.tagName == "INPUT" && element.type.toLowerCase() == "checkbox");
}
//get tree
function TV2_GetTreeById(id) {
return document.getElementById(id);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//private mothods, with TV2i_ prefix
//////////////////////////////////////////////////////////////////////////////////////////////
//get div contains child nodes
function TV2i_GetChildNodesDiv(node) {
//如果node.tagName == "A"則不處理
if (TV2_NodeIsLeaf(node))
return null;
var childNodsDivId = node.id + "Nodes";
return document.getElementById(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 || as.length == 0)
return null;
var regexp = new RegExp("^" + tree.id + "n\\d+$");
for (var i = 0; i < as.length; i++) {
if (as[i].id.match(regexp)) {
return as[i];
}
}
return null;
}
//find "INPUT" node
function TV2i_GetNodeInElementInput(tree, element) {
var as = WebForm_GetElementsByTagName(element, "INPUT");
if (as == null || as.length == 0)
return null;
var regexp = new RegExp("^" + tree.id + "n\\d+");
for (var i = 0; i < as.length; i++) {
if (as[i].id.match(regexp)) {
return as[i];
}
}
return null;
}
//get checkbox of node
function TV2i_NodeGetCheckBox(node) {
if (IsCheckBox(node))
return node;
var id = node.id + "CheckBox";
return document.getElementById(id);
}

html代碼 複製代碼 代碼如下:<asp:TreeView ID="TreeView1" runat="server" ImageSet="Msdn" ShowCheckBoxes="All"
ShowLines="True" BorderWidth="0px" Height="430px" Width="250px" Font-Size="Small"
OnClick="OnTreeNodeChecked()">
</asp:TreeView>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.