做权限的时候,主要实现如下功能
1、该节点可以访问,则他的父节点也必能访问;
2、该节点可以访问,则他的子节点也都能访问;
3、该节点不可访问,则他的子节点也不能访问。
使用带CheckBox的数型结构能得到很好的用户体验,可是编程的难度也是有点增加,如果全部有服务端来完成,那点下刷下,肯定不行,只能使用javascript,javascript调试的时候郁闷的很,一个字类,还有郁闷的递归,麻烦
我以前是使用以下该方法
<
script language
=
"
javascript
"
>
<!--
//
初始化节点
initchecknode(document.all(
"
tvItemClientID
"
).value,document.all(
"
checked
"
).value)
//
初始化选中节点
function
initchecknode(tree,tvvalue)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//获得需要初始化选择状态的节点的字符串.
var selectedNodes = tvvalue;
var arrayNodes = new Array();
arrayNodes = selectedNodes.split(",");
var AllRootNode=new Array();
AllRootNode=document.getElementById(tree).getChildren();
//初始化选择的节点
findAndCheckedNodes(AllRootNode,arrayNodes);
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
根据已选择的节点的字符串初始化选择的节点
function
findAndCheckedNodes(NodeArray,nodeDatas)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//alert(NodeArray.length);
if(parseInt(NodeArray.length)==0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(var i=0;i<parseInt(NodeArray.length);i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var cNode,nodeData;
cNode=NodeArray[i];
////如果该节点在nodeDatas里则初始化checked = true;
nodeData = cNode.getAttribute("NodeData");
for(var j=0;j<nodeDatas.length;j++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(nodeDatas[j] == nodeData)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cNode.setAttribute("checked","true");
break;
}
}
//如果有子节点,则继续递归
if(parseInt(cNode.getChildren().length)!=0)
findAndCheckedNodes(cNode.getChildren(),nodeDatas);
}
}
}
//
-->
//
oncheck事件
function
tree_oncheck(tree)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var i;
var node=tree.getTreeNode(tree.clickedNodeIndex);
var Pchecked=tree.getTreeNode(tree.clickedNodeIndex).getAttribute("checked");
setcheck(node,Pchecked);
setParent(node,Pchecked);
//window.alert(Pchecked);
document.all.checked.value = "";
if (tree.getChildren().length > 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (i=0;i<=tree.getChildren().length-1;i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tree.getChildren()[i].getAttribute("Checked"))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AddChecked(tree.getChildren()[i]);
}
FindCheckedFromNode(tree.getChildren()[i]);
}
}
}
//
设置子节点选中
function
setcheck(node,Pchecked)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var i;
var ChildNode=new Array();
ChildNode=node.getChildren();
if(parseInt(ChildNode.length)==0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(i=0;i<ChildNode.length;i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var cNode;
cNode=ChildNode[i];
cNode.setAttribute("Checked",Pchecked);
//cNode.Checked = Pchecked;
if(parseInt(cNode.getChildren().length)!=0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
setcheck(cNode,Pchecked);
}
}
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
设置子节点选中/取消;
//
同时需要设置父节点的状态(如果是取消选中的话,仅仅设置本节点及其所有字接点,不涉及父接点)
function
setParent(node,Pc)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var parentNode = node.getParent();
if(parentNode)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var parentNodeFather = parentNode.getParent();
if(parentNodeFather)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
setParent(parentNode,Pc);
}
if(Pc)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{parentNode.setAttribute("checked",Pc);}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
checkBrother(parentNode,Pc,node.getAttribute("NodeData"))
}
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
检查子接点是否有选择的,如果有一个选择了,那返回true
//
只查第一级节点.
function
checkBrother(parentNode,Pc,NodeData)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var childNodes = new Array();
childNodes = parentNode.getChildren();
if(childNodes.length >0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var bChecked = true;
for(var i=0;i<childNodes.length;i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(childNodes[i].getAttribute("checked") == true && childNodes[i].getAttribute("NodeData") != NodeData)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//alert(i+childNodes[i].getAttribute("Text"));
bChecked = false;
break;
}
}
if(bChecked)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parentNode.setAttribute("checked",Pc);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//所有父结点选择
setParent(parentNode,!Pc)
}
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parentNode.setAttribute("checked",Pc);
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
获取所有节点状态
function
FindCheckedFromNode(node)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var i = 0;
var nodes = new Array();
nodes = node.getChildren();
for (i = 0; i <= nodes.length - 1; i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (nodes[i].getAttribute("Checked"))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AddChecked(nodes[i]);
}
if (parseInt(nodes[i].getChildren().length) != 0 )
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FindCheckedFromNode(nodes[i]);
}
}
}
//
添加选中节点
function
AddChecked(node)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
document.all.checked.value += node.getAttribute("NodeData") + ",";
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
-->
</
script
>
这种方法有个很大的问题,就是他客户端设置的CheckBox在服务器端是不能获取的,现在只能在Check的时候遍历树,并把Checked的值放在一个文本里,然后提交到服务器,然后服务器来解析
1@2@这种字符
现在我使用的是asp.net2.0,使用的是以下的方法
function
public_GetParentByTagName(element, tagName)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
//如果这个元素还不是想要的tag就继续上溯
while (parent && (parent.tagName.toUpperCase() != upperTagName))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
设置节点的父节点Cheched——该节点可访问,则他的父节点也必能访问
function
setParentChecked(objNode)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var objParentDiv = public_GetParentByTagName(objNode,"div");
if(objParentDiv==null || objParentDiv == "undefined")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
var objID = objParentDiv.getAttribute("ID");
objID = objID.substring(0,objID.indexOf("Nodes"));
objID = objID+"CheckBox";
var objParentCheckBox = document.getElementById(objID);
if(objParentCheckBox==null || objParentCheckBox == "undefined")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox")
return;
objParentCheckBox.checked = true;
setParentChecked(objParentCheckBox);
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
设置节点的子节点uncheched——该节点不可访问,则他的子节点也不能访问
function
setChildUnChecked(divID)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i<objchild.length;i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var tempObj = objchild[i];
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tempObj.checked = false;
}
setChildUnChecked(tempObj);
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
设置节点的子节点cheched——该节点可以访问,则他的子节点也都能访问
function
setChildChecked(divID)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i<objchild.length;i++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var tempObj = objchild[i];
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tempObj.checked = true;
}
setChildChecked(tempObj);
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
触发事件
function
CheckEvent()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var objNode = event.srcElement;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(objNode.tagName!="INPUT" || objNode.type!="checkbox")
return;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(objNode.checked==true)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
setParentChecked(objNode);
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf("CheckBox"));
var objParentDiv = document.getElementById(objID+"Nodes");
if(objParentDiv==null || objParentDiv == "undefined")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
setChildChecked(objParentDiv);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf("CheckBox"));
var objParentDiv = document.getElementById(objID+"Nodes");
if(objParentDiv==null || objParentDiv == "undefined")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
setChildUnChecked(objParentDiv);
}
}
这种方法最大的好处就是服务端能得javascript设置的Checked的值,不用没点下就全部把树遍利下,直接在服务端遍利一次就行了
//
遍历子节点
public
void
GetChildNode(TreeNode Node)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
foreach (TreeNode node in Node.ChildNodes)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (node.Checked)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StrChecked += node.Value+"@";
}
GetChildNode(node);
}
}
}
我照样能得到它的Check的值