两个客户端操作TreeView节点CheckBox的小例子


下面是修改后详细代码:
<script language="javascript">

            
//初始化选中节点
            function initchecknode()
            
{
                
//获得需要初始化选择状态的节点的字符串.
                var selectedNodes = document.all.checkedNodes.value;
                
var arrayNodes = new Array();
                arrayNodes 
= selectedNodes.split(",");
                
var AllRootNode=new Array();
                AllRootNode
=document.getElementById("TreeView1").getChildren();
                
//初始化选择的节点
                findAndCheckedNodes(AllRootNode,arrayNodes);
            }


            
//根据已选择的节点的字符串初始化选择的节点
            function findAndCheckedNodes(NodeArray,nodeDatas)
            
{
                
//alert(NodeArray.length);
                 if(parseInt(NodeArray.length)==0)
                 
{
                    
return;
                }

                
else
                
{
                    
for(var i=0;i<parseInt(NodeArray.length);i++)
                    
{
                        
var cNode,nodeData;
                        cNode
=NodeArray[i];
                        
////如果该节点在nodeDatas里则初始化checked = true;
                        nodeData = cNode.getAttribute("NodeData");
                        
for(var j=0;j<nodeDatas.length;j++)
                        
{
                            
if(nodeDatas[j] == nodeData)
                            
{
                                cNode.setAttribute(
"checked","true");
                                
break;
                            }

                        }

                        
//如果有子节点,则继续递归
                        if(parseInt(cNode.getChildren().length)!=0)
                        findAndCheckedNodes(cNode.getChildren(),nodeDatas);    
                    }

                }

            }

            
            
//节点的oncheck事件
            function tree_oncheck(tree)
            
{
                
var node=tree.getTreeNode(tree.clickedNodeIndex);
                
var Pchecked=tree.getTreeNode(tree.clickedNodeIndex).getAttribute("checked");
                setcheck(node,Pchecked);
                document.all.checkedNodes.value
="";
                document.all.unchecked.value
="";
                FindCheckedFromNode(TreeView1);
            }

            
//设置子节点选中
            function setcheck(node,Pc)
            
{
                
var i;
                
var ChildNode=new Array();
                ChildNode
=node.getChildren();
                 
                
if(parseInt(ChildNode.length)==0)
                    
return;
                
else
                
{
                    
for(i=0;i<ChildNode.length;i++)
                    
{
                        
var cNode;
                        cNode
=ChildNode[i];
                        
if(parseInt(cNode.getChildren().length)!=0)
                            setcheck(cNode,Pc);
                        cNode.setAttribute(
"checked",Pc);
                    }

                }

            }

            
//获取所有节点状态
            function FindCheckedFromNode(node) 
            
{
                
var i = 0;
                
var nodes = new Array();
                nodes 
= node.getChildren();
                 
                
for (i = 0; i < nodes.length; i++
                
{
                    
var cNode;
                    cNode
=nodes[i];
                    
if (cNode.getAttribute("checked"))
                        AddChecked(cNode);
                    
else
                        AddUnChecked(cNode);
                      
                    
if (parseInt(cNode.getChildren().length) != 0 ) 
                    
{
                        FindCheckedFromNode(cNode);
                    }

                }

            }

            
//添加选中节点
            function AddChecked(node) 
            
{
                document.all.checkedNodes.value 
+= node.getAttribute("NodeData");
                document.all.checkedNodes.value 
+= ',';
            }

            
//添加未选中节点
            function AddUnChecked(node)
            
{
                document.all.unchecked.value 
+= node.getAttribute("NodeData");
                document.all.unchecked.value 
+= ',';
            }

            
        
</script>
posted @ 2006-11-11 10:24  '.Elvis.'  阅读(180)  评论(0编辑  收藏  举报