All of u thanks for the posts ..it helped me a lot in successfully completing my task
I wanted a tree view control where if Parent is checked or Unchecked the child nodes get checked and unchecked respectively also i wanted
if ANY one child is checked then parent should get checked and if ALL childs are unchecked then parent should get unchecked.
Following code does this
call this function "client_OnTreeNodeChecked()" on the Onclick event of treeview.
function client_OnTreeNodeChecked()
{
var obj = window.event.srcElement;
var treeNodeFound = false;
var checkedState;
if (obj.tagName == "INPUT" && obj.type == "checkbox") {
var treeNode = obj;
checkedState = treeNode.checked;
do
{
obj = obj.parentElement;
} while (obj.tagName != "TABLE")
var parentTreeLevel = obj.rows[0].cells.length;
var parentTreeNode = obj.rows[0].cells[0];
var tables = obj.parentElement.getElementsByTagName("TABLE");
if(obj.tagName == "TABLE")
{
if (treeNode.checked) //if any child is checked check parent
{
//head1 gets the parent node of the checked node
var head1 = obj.parentElement.previousSibling;
if(head1!=null)
{
if(head1.tagName == "TABLE")
{
//checks for the input tag which consists of checkbox
var matchElement1 = head1.getElementsByTagName("INPUT");
//matchElement1[0] gives us the checkbox and it is checked
if(matchElement1.length>0)
matchElement1[0].checked = true;
}
}
else {
head1 = obj.parentElement.previousSibling;
}
if(head1!=null)
{
if(head1.tagName == "TABLE")
{
//head2 gets the parent node of the checked node
var head2 = obj.parentElement.parentElement.previousSibling;
if(head2!=null)
{
if(head2.tagName == "TABLE")
{
//checks for the input tag which consists of checkbox
var matchElement2 = head2.getElementsByTagName("INPUT");
if(matchElement2.length>0)
matchElement2[0].checked = true;
}
}
}
}
else {
head2 = obj.parentElement.previousSibling;
}
if(head2!=null)
{
if(head2.tagName == "TABLE")
{
//head3 gets the parent node of the checked node
var head3 = obj.parentElement.parentElement.parentElement.previousSibling;
if(head3!=null)
{
if(head3.tagName == "TABLE")
{
//checks for the input tag which consists of checkbox
var matchElement3 = head3.getElementsByTagName("INPUT");
if(matchElement3.length>0)
matchElement3[0].checked = true;
}
}
}
}
else {
head3 = obj.parentElement.previousSibling;
}
if(head3!=null)
{
if(head3.tagName == "TABLE")
{
//head4 gets the parent node of the checked node
var head4 = obj.parentElement.parentElement.parentElement.parentElement.previousSibling;
if(head4 != null)
{
if(head4.tagName == "TABLE")
{
//checks for the input tag which consists of checkbox
var matchElement4 = head4.getElementsByTagName("INPUT");
if(matchElement4.length>0)
matchElement4[0].checked = true;
}
}
}
}
}//end if - checked
if (!treeNode.checked)//if all child are unchecked then uncheck parent
{
var chk1 = false;
var head1 = obj.parentElement.previousSibling;
var pTreeLevel1 = obj.rows[0].cells.length;
if(head1!=null)
{
if(head1.tagName == "TABLE")
{
var tbls = obj.parentElement.getElementsByTagName("TABLE");
var tblsCount = tbls.length;
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel1)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if(chld.length>0)
{
if (chld[0].checked == true)
{
chk1 = true;
break;
}
}
}
}
var nd = head1.getElementsByTagName("INPUT");
if(nd.length>0){
nd[0].checked = chk1;}
}
}
else
{
head1 = obj.parentElement.previousSibling;
}
var chk2 = false;
if(head1!=null)
{
if(head1.tagName == "TABLE")
{
var head2 = obj.parentElement.parentElement.previousSibling;
if(head2!=null)
{
if(head2.tagName == "TABLE")
{
var tbls = head1.parentElement.getElementsByTagName("TABLE");
var pTreeLevel2 = head1.rows[0].cells.length;
var tblsCount = tbls.length;
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel2)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if(chld.length>0)
{
if (chld[0].checked == true)
{
chk2 = true;
break;
}
}
}
}
var nd = head2.getElementsByTagName("INPUT");
if(nd.length>0){
nd[0].checked = (chk2 && chk1);}
}
}
}
}
else
{
head2 = obj.parentElement.previousSibling;
}
var chk3 = false;
if(head2!=null)
{
if(head2.tagName == "TABLE")
{
var head3 = obj.parentElement.parentElement.parentElement.previousSibling;
if(head3!=null)
{
if(head3.tagName == "TABLE")
{
var tbls = head2.parentElement.getElementsByTagName("TABLE");
var pTreeLevel3 = head2.rows[0].cells.length;
var tblsCount = tbls.length;
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel3)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if(chld.length>0)
{
if (chld[0].checked == true)
{
chk3 = true;
break;
}
}
}
}
var nd = head3.getElementsByTagName("INPUT");
if(nd.length>0){
nd[0].checked = (chk3 && chk2 && chk1);}
}
}
}
}
else
{
head3 = obj.parentElement.previousSibling;
}
var chk4 = false;
if(head3!=null)
{
if(head3.tagName == "TABLE")
{
var head4 = obj.parentElement.parentElement.parentElement.parentElement.previousSibling;
if(head4!=null)
{
if(head4.tagName == "TABLE")
{
var tbls = head3.parentElement.getElementsByTagName("TABLE");
var pTreeLevel4 = head3.rows[0].cells.length;
var tblsCount = tbls.length;
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel4)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if(chld.length>0)
{
if (chld[0].checked == true)
{
chk4 = true;
break;
}
}
}
}
var nd = head4.getElementsByTagName("INPUT");
if(nd.length>0){
nd[0].checked = (chk4 && chk3 && chk2 && chk1);}
}
}
}
}
}//end if - unchecked
}
var numTables = tables.length
if (numTables >= 1)
{
for (i=0; i < numTables; i++)
{
if (tables[i] == obj)
{
treeNodeFound = true;
i++;
if (i == numTables)
{
return;
}
}
if (treeNodeFound == true)
{
var childTreeLevel = tables[i].rows[0].cells.length;
if (childTreeLevel > parentTreeLevel)
{
var cell = tables[i].rows[0].cells[childTreeLevel - 1];
var inputs = cell.getElementsByTagName("INPUT");
inputs[0].checked = checkedState;
}
else
{
return;
}
}
}
}
}
}
Avanti
Member
2 Points
1 Post
Re: ASP.NET 2.0 Treeview Checkboxes - Check All - Javascript
Aug 01, 2007 06:56 AM|LINK
All of u thanks for the posts ..it helped me a lot in successfully completing my task
I wanted a tree view control where if Parent is checked or Unchecked the child nodes get checked and unchecked respectively also i wanted
if ANY one child is checked then parent should get checked and if ALL childs are unchecked then parent should get unchecked.
Following code does this
call this function "client_OnTreeNodeChecked()" on the Onclick event of treeview.
function client_OnTreeNodeChecked()
{
var obj = window.event.srcElement;
var treeNodeFound = false;
var checkedState;
if (obj.tagName == "INPUT" && obj.type == "checkbox") {
var treeNode = obj;
checkedState = treeNode.checked;
do
{
obj = obj.parentElement;
} while (obj.tagName != "TABLE")
var parentTreeLevel = obj.rows[0].cells.length;
var parentTreeNode = obj.rows[0].cells[0];
var tables = obj.parentElement.getElementsByTagName("TABLE");
if(obj.tagName == "TABLE")
{
if (treeNode.checked) //if any child is checked check parent
{
//head1 gets the parent node of the checked node
var head1 = obj.parentElement.previousSibling;
if(head1!=null)
{
if(head1.tagName == "TABLE")
{
//checks for the input tag which consists of checkbox
var matchElement1 = head1.getElementsByTagName("INPUT");
//matchElement1[0] gives us the checkbox and it is checked
if(matchElement1.length>0)
matchElement1[0].checked = true;
}
}
else {
head1 = obj.parentElement.previousSibling;
}
if(head1!=null)
{
if(head1.tagName == "TABLE")
{
//head2 gets the parent node of the checked node
var head2 = obj.parentElement.parentElement.previousSibling;
if(head2!=null)
{
if(head2.tagName == "TABLE")
{
//checks for the input tag which consists of checkbox
var matchElement2 = head2.getElementsByTagName("INPUT");
if(matchElement2.length>0)
matchElement2[0].checked = true;
}
}
}
}
else {
head2 = obj.parentElement.previousSibling;
}
if(head2!=null)
{
if(head2.tagName == "TABLE")
{
//head3 gets the parent node of the checked node
var head3 = obj.parentElement.parentElement.parentElement.previousSibling;
if(head3!=null)
{
if(head3.tagName == "TABLE")
{
//checks for the input tag which consists of checkbox
var matchElement3 = head3.getElementsByTagName("INPUT");
if(matchElement3.length>0)
matchElement3[0].checked = true;
}
}
}
}
else {
head3 = obj.parentElement.previousSibling;
}
if(head3!=null)
{
if(head3.tagName == "TABLE")
{
//head4 gets the parent node of the checked node
var head4 = obj.parentElement.parentElement.parentElement.parentElement.previousSibling;
if(head4 != null)
{
if(head4.tagName == "TABLE")
{
//checks for the input tag which consists of checkbox
var matchElement4 = head4.getElementsByTagName("INPUT");
if(matchElement4.length>0)
matchElement4[0].checked = true;
}
}
}
}
}//end if - checked
if (!treeNode.checked)//if all child are unchecked then uncheck parent
{
var chk1 = false;
var head1 = obj.parentElement.previousSibling;
var pTreeLevel1 = obj.rows[0].cells.length;
if(head1!=null)
{
if(head1.tagName == "TABLE")
{
var tbls = obj.parentElement.getElementsByTagName("TABLE");
var tblsCount = tbls.length;
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel1)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if(chld.length>0)
{
if (chld[0].checked == true)
{
chk1 = true;
break;
}
}
}
}
var nd = head1.getElementsByTagName("INPUT");
if(nd.length>0){
nd[0].checked = chk1;}
}
}
else
{
head1 = obj.parentElement.previousSibling;
}
var chk2 = false;
if(head1!=null)
{
if(head1.tagName == "TABLE")
{
var head2 = obj.parentElement.parentElement.previousSibling;
if(head2!=null)
{
if(head2.tagName == "TABLE")
{
var tbls = head1.parentElement.getElementsByTagName("TABLE");
var pTreeLevel2 = head1.rows[0].cells.length;
var tblsCount = tbls.length;
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel2)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if(chld.length>0)
{
if (chld[0].checked == true)
{
chk2 = true;
break;
}
}
}
}
var nd = head2.getElementsByTagName("INPUT");
if(nd.length>0){
nd[0].checked = (chk2 && chk1);}
}
}
}
}
else
{
head2 = obj.parentElement.previousSibling;
}
var chk3 = false;
if(head2!=null)
{
if(head2.tagName == "TABLE")
{
var head3 = obj.parentElement.parentElement.parentElement.previousSibling;
if(head3!=null)
{
if(head3.tagName == "TABLE")
{
var tbls = head2.parentElement.getElementsByTagName("TABLE");
var pTreeLevel3 = head2.rows[0].cells.length;
var tblsCount = tbls.length;
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel3)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if(chld.length>0)
{
if (chld[0].checked == true)
{
chk3 = true;
break;
}
}
}
}
var nd = head3.getElementsByTagName("INPUT");
if(nd.length>0){
nd[0].checked = (chk3 && chk2 && chk1);}
}
}
}
}
else
{
head3 = obj.parentElement.previousSibling;
}
var chk4 = false;
if(head3!=null)
{
if(head3.tagName == "TABLE")
{
var head4 = obj.parentElement.parentElement.parentElement.parentElement.previousSibling;
if(head4!=null)
{
if(head4.tagName == "TABLE")
{
var tbls = head3.parentElement.getElementsByTagName("TABLE");
var pTreeLevel4 = head3.rows[0].cells.length;
var tblsCount = tbls.length;
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel4)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if(chld.length>0)
{
if (chld[0].checked == true)
{
chk4 = true;
break;
}
}
}
}
var nd = head4.getElementsByTagName("INPUT");
if(nd.length>0){
nd[0].checked = (chk4 && chk3 && chk2 && chk1);}
}
}
}
}
}//end if - unchecked
}
var numTables = tables.length
if (numTables >= 1)
{
for (i=0; i < numTables; i++)
{
if (tables[i] == obj)
{
treeNodeFound = true;
i++;
if (i == numTables)
{
return;
}
}
if (treeNodeFound == true)
{
var childTreeLevel = tables[i].rows[0].cells.length;
if (childTreeLevel > parentTreeLevel)
{
var cell = tables[i].rows[0].cells[childTreeLevel - 1];
var inputs = cell.getElementsByTagName("INPUT");
inputs[0].checked = checkedState;
}
else
{
return;
}
}
}
}
}
}
Treeview