JavaScript如何控制Asp.Net TreeView 中的CheckBox的选中状态
首先我需要选中父节点!然后我当前父节点下面的子节点全部被选中!单机其他的以此类推!高分求一段代码!...
首先我需要选中父节点!然后我当前父节点下面的子节点全部被选中!单机其他的以此类推!高分求一段代码!
展开
1个回答
展开全部
<script language="javascript" type="text/javascript">
function HandleCheckbox() {
var element = event.srcElement;
if (element.tagName == "INPUT" && element.type == "checkbox") {
var checkedState = element.checked;
while (element.tagName != "TABLE") // Get wrapping table
{
element = element.parentElement;
}
var parentElement = element;
if (checkedState) {
CheckParents(element);
}
element = element.nextSibling; //element.tagName = DIV
if (element != null) // If no childrens then exit
{
var childTables = element.getElementsByTagName("TABLE");
for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++) {
CheckTable(childTables[tableIndex], checkedState);
}
}
if (checkedState == false) {
UnCheckParents(parentElement);
}
}
}
// Uncheck the parents of the given table, Can remove the recurse (redundant)
function CheckParents(table) {
if (table == null || table.rows[0].cells.length == 2) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
CheckTable(parentTable, true);
CheckParents(parentTable);
}
// Check the parents of the given table, Can remove the recurse (redundant)
function UnCheckParents(table) {
if (table == null || table.rows[0].cells.length == 2) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
var checkedCount = GetCheckedCount(table.parentElement);
if (checkedCount == 0) {
CheckTable(parentTable, false);
}
UnCheckParents(parentTable);
}
// Handle the set of checkbox checked state
function CheckTable(table, checked) {
var checkboxIndex = table.rows[0].cells.length - 1;
var cell = table.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1) {
checkboxes[0].checked = checked;
}
}
//Get checked children count
function GetCheckedCount(table) {
var checkedCount = 0;
var element = table.nextSibling;
var childTable = table.getElementsByTagName("TABLE");
for (var tableIndex = 0; tableIndex < childTable.length; tableIndex++) {
var childTables = childTable[tableIndex];
var checkboxIndex = childTables.rows[0].cells.length - 1;
var cell = childTables.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1 && checkboxes[0].checked == true) {
checkedCount++;
}
}
return checkedCount;
}
</script>
<asp:TreeView ID="roles" onclick="HandleCheckbox();" runat="server" ShowCheckBoxes="All"
BorderWidth="0" Width="350px" ExpandDepth="1" BorderStyle="None">
</asp:TreeView>
function HandleCheckbox() {
var element = event.srcElement;
if (element.tagName == "INPUT" && element.type == "checkbox") {
var checkedState = element.checked;
while (element.tagName != "TABLE") // Get wrapping table
{
element = element.parentElement;
}
var parentElement = element;
if (checkedState) {
CheckParents(element);
}
element = element.nextSibling; //element.tagName = DIV
if (element != null) // If no childrens then exit
{
var childTables = element.getElementsByTagName("TABLE");
for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++) {
CheckTable(childTables[tableIndex], checkedState);
}
}
if (checkedState == false) {
UnCheckParents(parentElement);
}
}
}
// Uncheck the parents of the given table, Can remove the recurse (redundant)
function CheckParents(table) {
if (table == null || table.rows[0].cells.length == 2) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
CheckTable(parentTable, true);
CheckParents(parentTable);
}
// Check the parents of the given table, Can remove the recurse (redundant)
function UnCheckParents(table) {
if (table == null || table.rows[0].cells.length == 2) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
var checkedCount = GetCheckedCount(table.parentElement);
if (checkedCount == 0) {
CheckTable(parentTable, false);
}
UnCheckParents(parentTable);
}
// Handle the set of checkbox checked state
function CheckTable(table, checked) {
var checkboxIndex = table.rows[0].cells.length - 1;
var cell = table.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1) {
checkboxes[0].checked = checked;
}
}
//Get checked children count
function GetCheckedCount(table) {
var checkedCount = 0;
var element = table.nextSibling;
var childTable = table.getElementsByTagName("TABLE");
for (var tableIndex = 0; tableIndex < childTable.length; tableIndex++) {
var childTables = childTable[tableIndex];
var checkboxIndex = childTables.rows[0].cells.length - 1;
var cell = childTables.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1 && checkboxes[0].checked == true) {
checkedCount++;
}
}
return checkedCount;
}
</script>
<asp:TreeView ID="roles" onclick="HandleCheckbox();" runat="server" ShowCheckBoxes="All"
BorderWidth="0" Width="350px" ExpandDepth="1" BorderStyle="None">
</asp:TreeView>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询