树型菜单控制Javascript

 javascript控制权限原理:
把功能模块存储在数据库中,建立用户并授权相应功能模块,
用户登录成功后,从数据库读入授权模块
实现:
  根据上下层,父与子的层次关系,建立好模块,
以树型控件表现出来,授权操作时较清晰,通过选取树结点的checkbox,
通过js控制保存到隐藏控件中,最后保存回服务器

脚本代码:
<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("tv").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);
    setParent(node,Pchecked);//如果是选择那么选择其父接点.
    document.all.checkedNodes.value="";
    document.all.unchecked.value="";
    FindCheckedFromNode(tv);
   }
   
//设置子节点选中/取消;
//同时需要设置父节点的状态(如果是取消选中的话,仅仅设置本节点及其所有字接点,不涉及父接点)
function setParent(node,Pc)
   {
    var parentNode = node.getParent();
    if(parentNode)
    {    
     var parentNodeFather = parentNode.getParent();
     if(parentNodeFather)
     {
      setParent(parentNode,Pc);
     }
     if(Pc)
      {parentNode.setAttribute("checked",Pc);}
     else
     {
      checkBrother(parentNode,Pc,node.getAttribute("NodeData"))
     }
    }
   }
   
//检查子接点是否有选择的,如果有一个选择了,那返回true
//只查第一级节点.
   function checkBrother(parentNode,Pc,nodeData)
   {
    var childNodes = new Array();
    childNodes = parentNode.getChildren();
    if(childNodes.length >0)
    {
     var bChecked = true;
     for(var i=0;i<childNodes.length;i++)
     {
      if(childNodes[i].getAttribute("checked") == true &&  childNodes[i].getAttribute("NodeData") != nodeData)
      {
       //alert(i+childNodes[i].getAttribute("Text"));
       bChecked = false;
       break;
      }
     }
     if(bChecked)
     {
      parentNode.setAttribute("checked",Pc);
     }
     else
     {
      //所有父结点选择
      setParent(parentNode,!Pc)
     }
    }
    else
    {
     parentNode.setAttribute("checked",Pc);
    }
   }

 
//设置子节点选中
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>

你可能感兴趣的:(JavaScript,数据库,function,服务器,tree)