现在的位置: 首页 > 综合 > 正文

JS实现选中某个节点其所有父节点也选中,取消某个节点,其叶子节点取消

2014年02月10日 ⁄ 综合 ⁄ 共 2220字 ⁄ 字号 评论关闭
var aStack=new Stack();

		var RequireEvent=true;  //是否重复调用Checked事件

		function UltraWebTree1_NodeChecked(treeId, nodeId, bChecked)

		{

			var node = igtree_getNodeById(nodeId);  //获取选中的节点(父节点)

			if(RequireEvent==true)

				setcheck(node,bChecked);

		}

		

		function RecusiveCheckChildrenNode(node,Pc)

		{

			if(Pc==false)

			{

				var ChildNode=node.getChildNodes(); //获取选中节点的子节点



				if(parseInt(ChildNode.length)==0) //不存在子节点

				{ 

					return;

				} 

				else 

				{ 

					aStack.RemoveElementByKey(node.getTag());  //移除取消选中的节点

					for(var i=0;i<ChildNode.length;i++)    //遍历子节点

					{ 

						var cNode; 

						cNode=ChildNode[i]; 

						cNode.setChecked(Pc);

						aStack.RemoveElementByKey(cNode.getTag());//移除取消选中的部门节点

						if(parseInt(cNode.getChildNodes().length)!=0)   //子节点的子节点

							RecusiveCheckChildrenNode(cNode,Pc);           



						

					} 

				} 

			}

		}

		//选中节点时所有父节点选中,取消选中节点时所有子节点取消选中

		function setcheck(node,Pc) 

		{ 

			var ParentNode;

			ParentNode=node;

			RequireEvent=false;

			if(Pc==true)  //得到选中节点的所有父节点

			{

				while(ParentNode!=null)

				{

					

					ParentNode.setChecked(Pc);

					if(!aStack.Contain(ParentNode.getTag()))

						aStack.Push(ParentNode.getTag());//选中的部门ID进入堆栈

					ParentNode=ParentNode.getParent();

				}

			}

			else

			{

				//取消选中节点的子节点

				RecusiveCheckChildrenNode(node,false);

				

			}

			RequireEvent=true;

			Form1.hdFID.value=aStack.ToString();

			//alert(Form1.hdFID.value);

			

		}









/*堆栈操作类,提供堆栈的进、出、移除指定元素、移除指定索引的元素、转换成String等方法*/

function Stack()

{

	this.MemoryList=new Array();

	this.Push=function(aElement)

	{

		this.MemoryList.push(aElement);

	}

	this.Pop=function()

	{

		return this.MemoryList.pop();

	}

	this.ToString=function()

	{

		return this.MemoryList.toString();

	}

	this.Contain=function(aElement)

	{

		for(var i=0;i<this.MemoryList.length;i++)

		{

			if(this.MemoryList[i]==aElement)

			{

				return true;

			}

		}

		return false;

	}

	this.RemoveElementByIndex=function(index)

	{

		try

		{

			if(index>=0&&this.MemoryList.length>=index)

			{

				for(var i=index;i<this.MemoryList.length-1;i++)

					this.MemoryList[i]=this.MemoryList[i+1];

				if(this.MemoryList.length>=2)

					this.MemoryList=this.MemoryList.splice(0,this.MemoryList.length-1);

				else

					this.MemoryList.pop();

				return true;

			}

		}

		catch(E)

		{

			return false;

		}

	}

	this.Clear=function()

	{

		this.MemoryList=null;

		this.MemoryList=new Array();

	}

	this.RemoveElementByKey=function(aElement)

	{

		var index;

		var TmpArray=new Array();

		for(var i=0;i<this.MemoryList.length;i++)

		{

			if(this.MemoryList[i]==aElement)

			{

				TmpArray[TmpArray.length]=i;

				

				

			}

		}

		try

		{

			for(index=TmpArray.length-1;index>=0;index--)

			{

				this.RemoveElementByIndex(TmpArray[index]);

			}

			return true;

		}

		catch(E)

		{

			throw E;

			return false;

		}

		

	}

}

 

【上篇】
【下篇】

抱歉!评论已关闭.