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


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

    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被选节点:

    代码
     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源码:

    代码
    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):

    代码
    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;   
       }   
    }   

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

    代码
     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     }
  • 相关阅读:
    关于事件的分发与传递
    Android直播app用什么技术可以做到延迟小一些?
    android菜鸟求教eclipse创建avd问题
    安卓APP集成支付宝,调用支付接口,直接退出程序了
    关于蓝牙socket 服务器端首次accept总出错!
    【求助】Android 4.4.2 的WebView的问题
    android 4.0.3开机时不能收到广播
    vue 应用 :关于 ElementUI 的 message 组件
    vue 应用 : 计数器组件
    vue 应用 :多语言显示
  • 原文地址:https://www.cnblogs.com/alonghay/p/1814517.html
Copyright © 2020-2023  润新知