用客户端JS实现微软TreeView控件父子节点联选!

      说明
      TreeView控件应该说是比较常用的一个东东了.别人提供的JSTree实现(如Yahoo UI lIb) 都有这样或那样的问题而微软的TreeView控件虽然用起来方便,可是一直感觉也不是很理想.比如,无实通过属性设定为是否父子节点联选(既,选中父节点就选中所有子节点或孙节点,反之,全部取消选择了子节点父节点也要被取消选择).以前1.1的时候,可以用更改HtC的方法搞定.2.0下,JS是编在DLL里的,所以只能自己通过客户端JS搞定.

       解决思路
      先说一下TreeView在IE中实现的结构,
      <Div   TreeViewID>
             <CheckBox  id = TreeViewID +  n +  节点次序/>
             <span  id=TreeViewID + t  + 节点次序">节点的Text</span>
             --如果有子节点
            <Div  ID = TreeViewID + n  + 节点次序 + Nodes>
                  同样的次序
            </Div>
     </Div>
    
    有了这个就好办了,先拦下TreeView的OnClick事件,判断事件源是不是CheckBox,如果是,就按这个次序寻根认祖, 再取设定子节点的状态, 如果是取消选定,还要判断一下是不是所有兄弟结点都被取消选定了,如果是这样,那也要取消父节点.

  后台拦下TreeView的OnClick事件很简单
       

None.gif  TreeViewID.Attributes.Add( " onclick " " HandleCheckEvent() " ); 



前台JS不多解释了,思路有了,看代码吧

None.gif      < script >
None.gif    
None.gif    
function  setParentState(objNode)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
var objParentDiv = WebForm_GetParentByTagName(objNode,"div");
InBlock.gif        
if(objParentDiv == null || objParentDiv == "undefined")
InBlock.gif            
return;
InBlock.gif            
InBlock.gif        
var divID = objParentDiv.getAttribute("ID");
InBlock.gif        
var prefix = divID.substring(0,divID.indexOf("Nodes"));
InBlock.gif        
var parentID = prefix + "CheckBox";
InBlock.gif        
InBlock.gif        
var parentChk = document.getElementById(parentID);
InBlock.gif        
if(parentChk == null || parentChk == "undefined")
InBlock.gif            
return;
InBlock.gif
InBlock.gif        
if (objNode.checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            parentChk.checked 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (isAllChildrenUnChecked(parentChk))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                parentChk.checked 
= false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
InBlock.gif        setParentState(parentChk);
ExpandedBlockEnd.gif    }

None.gif    
None.gif    
function  setChildState(objNode) // 设定子控件状态
ExpandedBlockStart.gifContractedBlock.gif
     dot.gif {
InBlock.gif        
var nodeID = objNode.getAttribute("ID");   //chkBox ID 
InBlock.gif
        var prefix = nodeID.substring(0,nodeID.indexOf("CheckBox"));  //节点的前缀
InBlock.gif
        var childrenDiv = document.getElementById(prefix + "Nodes");
InBlock.gif        
InBlock.gif        
if(childrenDiv == null || childrenDiv == "undefined")
InBlock.gif            
return ;
InBlock.gif             
InBlock.gif        
var childrenArray = childrenDiv.children; //取得所有子控件
InBlock.gif

InBlock.gif        
for(var i = 0;i< childrenArray.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var container = childrenArray[i]; //子控件的容器
InBlock.gif
            var chk = WebForm_GetElementByTagName(container,"input"); //查找Check控件,由于只有一个种Input控件,就是CheckBox
InBlock.gif
            chk.checked = objNode.checked;
InBlock.gif                   
InBlock.gif            setChildState(chk);
InBlock.gif                       
ExpandedSubBlockEnd.gif        }

InBlock.gif            
ExpandedBlockEnd.gif    }

None.gif    
None.gif    
function  isAllChildrenUnChecked(objChk)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
var objChkID = objChk.getAttribute("ID");
InBlock.gif        
var prefix = objChkID.substring(0,objChkID.indexOf("CheckBox"));  //节点的前缀
InBlock.gif
        var childrenDiv = document.getElementById(prefix + "Nodes");
InBlock.gif        
InBlock.gif         
if(childrenDiv == null || childrenDiv == "undefined")
InBlock.gif            
return ;
InBlock.gif            
InBlock.gif        
var childrenArray = childrenDiv.children; //取得所有子控件
InBlock.gif
        
InBlock.gif        
for(var i = 0;i< childrenArray.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var container = childrenArray[i]; //子控件的容器
InBlock.gif
            var chk = WebForm_GetElementByTagName(container,"input"); //查找Check控件,由于只有一个种Input控件,就是CheckBox
InBlock.gif
           
InBlock.gif            
if (chk.checked)
InBlock.gif                
return false;
InBlock.gif                       
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
return true;
ExpandedBlockEnd.gif    }

None.gif    
None.gif
None.gif    
// 触发事件
None.gif
     function  HandleCheckEvent()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif        
var objNode = event.srcElement; 
InBlock.gif
InBlock.gif        
if(objNode.tagName !="INPUT" || objNode.type!="checkbox")
InBlock.gif            
return;
InBlock.gif         
InBlock.gif         
//设定子Chk状态
InBlock.gif
        setChildState(objNode);
InBlock.gif        
//设定父Chk状态
InBlock.gif
        setParentState(objNode);
InBlock.gif
InBlock.gif      
ExpandedBlockEnd.gif    }

None.gif
None.gif
None.gif
None.gif    
</ script >


 

 

 

转载于:https://www.cnblogs.com/listhome/archive/2006/12/31/608746.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值