treeview十八般武艺,js选择和绑定权限树

最佳JS实现treeview权限树遍历方法(当然,要以客户需求为主):

 

ExpandedBlockStart.gif JS
 1      function getParentByTagName(element,tagName)
 2      {
 3          var    parent  =  element.parentNode;
 4          var    upperTagName  =  tagName.toUpperCase();
 5           while  (parent  &&  (parent.tagName.toUpperCase()  !=  upperTagName))
 6          {
 7              parent  =  parent.parentNode  ?  parent.parentNode : parent.parentElement;
 8          }
 9           return  parent;
10      }
11 
12      function setParentChecked(objNode)
13      {
14          var    objParentDiv  =  getParentByTagName(objNode, " div " );
15           if (objParentDiv  ==   null   ||  objParentDiv  ==   " undefined " )
16               return ;
17          var    objID  =     objParentDiv.getAttribute( " ID " );
18          var    objParentCheckBox  =     document.getElementById(objID.replace( " Nodes " , " CheckBox " ));
19           if (objParentCheckBox  ==   null   ||  objParentCheckBox     ==   " undefined " )
20               return ;
21           if (objParentCheckBox.tagName != " INPUT "   &&  objParentCheckBox.type     ==   " checkbox " )
22               return ;
23         
24           // objParentCheckBox.checked = true;
25           if (objParentCheckBox.value  ==   " 117 "
26          {
27               // alert("ok");
28              objParentCheckBox. checked = false ;
29          }
30           else
31          {
32               if  (objNode. checked = true ) objParentCheckBox. checked = true ;
33          }
34          
35          setParentChecked(objParentCheckBox);
36      }
37      function setParentUnChecked(objNode)
38      {
39          var    objParentDiv  =  getParentByTagName(objNode, " div " );
40           if (objParentDiv  ==   null   ||  objParentDiv  ==   " undefined " )
41               return ;
42          var    objID  =     objParentDiv.getAttribute( " ID " );
43          var    objParentCheckBox  =     document.getElementById(objID.replace( " Nodes " , " CheckBox " ));
44           if (objParentCheckBox  ==   null   ||  objParentCheckBox     ==   " undefined " )
45               return ;
46           if (objParentCheckBox.tagName != " INPUT "   &&  objParentCheckBox.type     ==   " checkbox " )
47               return ;
48           if  (objNode. checked = false ) objParentCheckBox. checked = false ;
49          objParentCheckBox. checked   =   false ;
50          setParentUnChecked(objParentCheckBox);
51      }
52 
53      function setChildCheckedState(div,state)
54      {
55          var    objchild  =  div.childNodes;
56          var    count  =     objchild.length;
57           for (var    i = 0 ;i < objchild.length;i ++ )
58          {
59              var    tempObj     =  objchild[i];
60               if (tempObj.tagName == " INPUT "      &&  tempObj.type     ==   " checkbox " )
61              {
62                  tempObj. checked      =  state;
63              }
64  //                  debugger;
65              setChildCheckedState(tempObj,state);
66          }
67      }
68      function TreeNodeChecked()
69      {
70          var    objNode     =  window. event .srcElement;
71           if (objNode.tagName != " INPUT "      ||  objNode.type != " checkbox " )
72               return ;
73  //              debugger;
74           if (objNode. checked   ==   true )
75          {
76              setParentChecked(objNode);
77          }
78           else
79          {
80               // setParentUnChecked(objNode);
81              
82          }
83          var    objID  =     objNode.getAttribute( " ID " );
84          var    objParentDiv  =  document.getElementById(objID.replace( " CheckBox " , " Nodes " ));
85           if (objParentDiv == null   ||   typeof (objParentDiv)  ==   " undefined " )
86               return ;
87          setChildCheckedState(objParentDiv,objNode. checked );
88 
89      }
90 
91  </ script >

 

 

后台递归遍历绑定treeview被选节点:

 

ExpandedBlockStart.gif 代码
 1       ///   <summary>
 2       ///  判断TreeView被选节点
 3       ///   </summary>
 4       ///   <param name="tv"> TreeView名称 </param>
 5       private   void  InitTreeChecked(TreeNode parentID,  string  menuID)
 6      {
 7 
 8           if  (parentID.Value  ==  menuID) // 根节点
 9          {
10              parentID.Checked  =   true ;
11               // parentID = tvMenu.Nodes[0];
12          }
13           else
14          {
15               if  (parentID.ChildNodes.Count  !=   0 )
16              {
17                   // for (int i = 0; i < parentID.ChildNodes.Count; i++) // 一级子节点
18                   foreach (TreeNode childID  in  parentID.ChildNodes)
19                  {                 
20                           // if (parentID.ChildNodes[i].Value.Trim().ToString() == menuID.Trim())
21                           if (childID.Value.Trim().ToString()  ==  menuID.Trim().ToString())
22                          {
23                              childID.Checked  =   true ;
24                               return ;
25                          }
26                           else
27                          {
28                               // if (parentID.ChildNodes[parentID.ChildNodes.Count-1].Value.Trim().ToString())
29 
30                              parentID  =  childID;
31                               this .InitTreeChecked(parentID, menuID);                            
32                          }                        
33                   }                 
34              }        
35               else {} 
36          }
37      }

 

 

 

07年实现新添treeview权限的JS源码:

 

ExpandedBlockStart.gif 代码
function getParentByTagName(element,tagName)   
        {   
            var    parent 
=  element.parentNode;   
            var    upperTagName 
=  tagName.toUpperCase();   
            
while  (parent  &&  (parent.tagName.toUpperCase()  !=  upperTagName))   
            {   
                parent 
=  parent.parentNode  ?  parent.parentNode : parent.parentElement;   
            }   
            
return  parent;   
        }   
  
        function setParentChecked(objNode)   
        {   
            var    objParentDiv 
=  getParentByTagName(objNode, " div " );   
            
if (objParentDiv  ==   null   ||  objParentDiv  ==   " undefined " )   
                
return ;   
            var    objID 
=     objParentDiv.getAttribute( " ID " );   
            var    objParentCheckBox 
=     document.getElementById(objID.replace( " Nodes " , " CheckBox " ));   
            
if (objParentCheckBox  ==   null   ||  objParentCheckBox     ==   " undefined " )   
                
return ;   
            
if (objParentCheckBox.tagName != " INPUT "   &&  objParentCheckBox.type     ==   " checkbox " )   
                
return ;   
                
// add    
                  
                   
//                 if (objNode.checked=false) objPraentCheckBox.checked=false;   
//             objParentCheckBox.checked =    true;   
            setParentChecked(objParentCheckBox);   
        }   
        function setParentUnChecked(objNode)   
        {   
            var    objParentDiv 
=  getParentByTagName(objNode, " div " );   
            
if (objParentDiv  ==   null   ||  objParentDiv  ==   " undefined " )   
                
return ;   
            var    objID 
=     objParentDiv.getAttribute( " ID " );   
            var    objParentCheckBox 
=     document.getElementById(objID.replace( " Nodes " , " CheckBox " ));   
            
if (objParentCheckBox  ==   null   ||  objParentCheckBox     ==   " undefined " )   
                
return ;   
            
if (objParentCheckBox.tagName != " INPUT "   &&  objParentCheckBox.type     ==   " checkbox " )   
                
return ;   
                
// add    
                  
                   
//                 if (objNode.checked=false) objPraentCheckBox.checked=false;   
            objParentCheckBox. checked   =   false ;   
            setParentUnChecked(objParentCheckBox);   
        }   
  
        function setChildCheckedState(div,state)   
        {   
            var    objchild 
=  div.childNodes;   
            var    count 
=     objchild.length;   
            
for (var    i = 0 ;i < objchild.length;i ++ )   
            {   
                var    tempObj    
=  objchild[i];   
                
if (tempObj.tagName == " INPUT "      &&  tempObj.type     ==   " checkbox " )   
                {   
                    tempObj.
checked      =  state;   
                }   
//                  debugger;   
                setChildCheckedState(tempObj,state);   
            }   
        }   
        function TreeNodeChecked()   
        {   
            var    objNode    
=  window. event .srcElement;   
            
if (objNode.tagName != " INPUT "      ||  objNode.type != " checkbox " )   
                
return ;   
//              debugger;   
             if (objNode. checked   ==   true )   
            {   
                setParentChecked(objNode);   
            }   
            
else   
            {   
            setParentUnChecked(objNode);   
            }   
            var    objID 
=     objNode.getAttribute( " ID " );   
            var    objParentDiv 
=  document.getElementById(objID.replace( " CheckBox " , " Nodes " ));   
            
if (objParentDiv == null   ||   typeof (objParentDiv)  ==   " undefined " )   
                
return ;   
            setChildCheckedState(objParentDiv,objNode.
checked );   
  
        }  

 

07年JS实现更新treeview权限树(建议后台遍历treenodes):

 

ExpandedBlockStart.gif 代码
function getParentByTagName(element,tagName)   
        {   
            var    parent 
=  element.parentNode;   
            var    upperTagName 
=  tagName.toUpperCase();   
            
while  (parent  &&  (parent.tagName.toUpperCase()  !=  upperTagName))   
            {   
                parent 
=  parent.parentNode  ?  parent.parentNode : parent.parentElement;   
            }   
            
return  parent;   
        }   
  
        function setParentChecked(objNode)   
        {   
            var    objParentDiv 
=  getParentByTagName(objNode, " div " );   
            
if (objParentDiv  ==   null   ||  objParentDiv  ==   " undefined " )   
                
return ;   
            var    objID 
=     objParentDiv.getAttribute( " ID " );   
            var    objParentCheckBox 
=     document.getElementById(objID.replace( " Nodes " , " CheckBox " ));   
            
if (objParentCheckBox  ==   null   ||  objParentCheckBox     ==   " undefined " )   
                
return ;   
            
if (objParentCheckBox.tagName != " INPUT "   &&  objParentCheckBox.type     ==   " checkbox " )   
                
return ;   
                
// add    
                  
                   
//                 if (objNode.checked=false) objPraentCheckBox.checked=false;   
//             objParentCheckBox.checked =    true;   
            setParentChecked(objParentCheckBox);   
        }   
        function setParentUnChecked(objNode)   
        {   
            var    objParentDiv 
=  getParentByTagName(objNode, " div " );   
            
if (objParentDiv  ==   null   ||  objParentDiv  ==   " undefined " )   
                
return ;   
            var    objID 
=     objParentDiv.getAttribute( " ID " );   
            var    objParentCheckBox 
=     document.getElementById(objID.replace( " Nodes " , " CheckBox " ));   
            
if (objParentCheckBox  ==   null   ||  objParentCheckBox     ==   " undefined " )   
                
return ;   
            
if (objParentCheckBox.tagName != " INPUT "   &&  objParentCheckBox.type     ==   " checkbox " )   
                
return ;   
                
// add    
                  
                   
//                 if (objNode.checked=false) objPraentCheckBox.checked=false;   
            objParentCheckBox. checked   =   false ;   
            setParentUnChecked(objParentCheckBox);   
        }   
  
        function setChildCheckedState(div,state)   
        {   
            var    objchild 
=  div.childNodes;   
            var    count 
=     objchild.length;   
            
for (var    i = 0 ;i < objchild.length;i ++ )   
            {   
                var    tempObj    
=  objchild[i];   
                
if (tempObj.tagName == " INPUT "      &&  tempObj.type     ==   " checkbox " )   
                {   
                    tempObj.
checked      =  state;   
                }   
//                  debugger;   
                setChildCheckedState(tempObj,state);   
            }   
        }   
        function TreeNodeChecked()   
        {   
            var    objNode    
=  window. event .srcElement;   
            
if (objNode.tagName != " INPUT "      ||  objNode.type != " checkbox " )   
                
return ;   
//              debugger;   
             if (objNode. checked   ==   true )   
            {   
                setParentChecked(objNode);   
            }   
            
else   
            {   
            setParentUnChecked(objNode);   
            }   
            var    objID 
=     objNode.getAttribute( " ID " );   
            var    objParentDiv 
=  document.getElementById(objID.replace( " CheckBox " , " Nodes " ));   
            
if (objParentDiv == null   ||   typeof (objParentDiv)  ==   " undefined " )   
                
return ;   
            setChildCheckedState(objParentDiv,objNode.
checked );   
  
        }   
         function SetTreeNodeChecked(objNode1)   
        {   
            var    objNode    
= objNode1;   
             
            var    objID 
=     objNode.getAttribute( " ID " );   
            var    objParentDiv 
=  document.getElementById(objID.replace( " CheckBox " , " Nodes " ));   
            
if (objParentDiv == null   ||   typeof (objParentDiv)  ==   " undefined " )   
                
return ;   
            setChildCheckedState(objParentDiv,objNode.
checked );   
  
        }   
        function GetYHQS(id)   
        {   
           
        PageMethods.CallYHQX(id,callsuccessed);   
        }   
        function callsuccessed(result)   
        {   
//        // 循环页面   
// debugger;   
for  (i = 0 ;i < document.form1.length ;i ++ )   
        {   
           var objNode
= document.form1.elements[i];   
           
if  (objNode.tagName == " INPUT "      &&  objNode.type == " checkbox " )   
           {   
               objNode.
checked = false ;   
            }   
  
        }   
           
       
for  (i = 0 ;i < document.form1.length ;i ++ )   
        {   
           var objNode
= document.form1.elements[i];   
           
if  (objNode.tagName == " INPUT "      &&  objNode.type == " checkbox " )   
           {   
               
// 找到   
               
// 比较   
                if  (result.indexOf(objNode.title) !=- 1 )   
               {   
               objNode.
checked = true ;   
               SetTreeNodeChecked(objNode);   
               }   
           }   
  
        }   
        }   
       function test()   
       {   
       debugger;   
       
// 循环页面   
        for  (i = 0 ;i < document.form1.length ;i ++ )   
{   
   var objNode
= document.form1.elements[i];   
   
if  (objNode.tagName == " INPUT "      &&  objNode.type == " checkbox " )   
   {   
   
// 找到   
   
// 比较   
   objNode. checked = true ;   
   }   
}   

 

同样效果后台遍历代码如下:

 

ExpandedBlockStart.gif 代码
 1       ///   <summary>
 2       ///  读取treeview上所有节点值
 3       ///   </summary>
 4       ///   <param name="id"></param>
 5      ArrayList arry  =   new  ArrayList();
 6       private   void  GetAllValuesOfTreeview()
 7      {
 8          arry.Clear();
 9           for  ( int  i  =   0 ; i  <  tvMenu.CheckedNodes.Count; i ++ )
10          {
11              arry.Add(tvMenu.CheckedNodes[i].Value.Trim());
12          }
13      }

 

 

转载于:https://www.cnblogs.com/alonghay/archive/2010/09/01/1814517.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值