JS带选择框的树


带选择框的树。效果图:



主要函数介绍:

1、实例化树:var tree = new MyTree("Tree1", true, -1); 

参数介绍:第一个参数是树的名称。第二个参数是带不带选择框,第三个参数是第一个父结点的ID。


2、增加树节点:tree.Add(0, -1, "区域设置","","","treestyle/folder.png"); //nodeId, nodePId, nodeTxt, nodeUrl, nodeTarget, nodeImg


3、显示树:tree.Show();


4、展开所有结点:tree.FullExpand();


5、收缩所有结点:tree.FullCollapse();


6、选择所有结点:tree.CheckedAll(true);  //传入参数 false, 取消所有结点;



使用举例:

var tree = new MyTree("Tree1", true, -1);
tree.Add(0, -1, "区域设置","","","treestyle/folder.png");
//nodeId, nodePId, nodeTxt, nodeUrl, nodeTarget, nodeImg
tree.Add(1,0,"中国","","","treestyle/folder.png");
tree.Add(2,0,"美国","","","treestyle/folder.png");
tree.Add(3,1,"四川","","","treestyle/folder.png");
tree.Add(4,3,"成都","http://www.sina.com","_blank","treestyle/folder.png");
tree.Add(5,1,"重庆","","","");
tree.Add(6,3,"南充","","","");
tree.Add(7,2,"纽约","","","");
tree.Add(8,2,"纽约8","","","");
tree.Add(9,2,"纽约9","","","treestyle/folder.png");
tree.Add(10,2,"纽约10","","","treestyle/folder.png");
tree.Add(11,2,"纽约11","","","");
tree.Add(12,2,"纽约12","","","treestyle/folder.png");
tree.Add(13,4,"皮县","","","");
tree.Add(14,4,"双流","","","");
tree.Add(15,4,"新都","","","");
tree.Add(16,4,"青白江","","","");
tree.Add(17,4,"温江","","","");
tree.Add(18,6,"南部","","","");
tree.Add(19,6,"阆中","","","");
tree.Add(20,6,"仪陇","","","treestyle/folder.png");
tree.Add(21,6,"西充","","","");

tree.Show();


JS源码:

/*
	
*/

var TREENODE_LEVEL_WIDTH = 20;  
 
function MyTreeNode(nodeId, nodePId, nodeTxt, nodeUrl, nodeTarget, nodeImg){
    this.NodeType = 1;
	this.NodeId = nodeId;
	this.NodePId = nodePId;
	this.NodeTxt = nodeTxt;
	this.NodeUrl = nodeUrl;
	this.NodeTarget = nodeTarget;
	this.NodeImg = (nodeImg.length > 0) ? (WEBURL+ nodeImg) : "";
	
	this.NodeLevel = 0;
	this.HasChildren = false;
	this.NodeExpanded = false;
	this.NodeChecked = 0; //0=false,1=true,2=part
	
	//HTML
	this.DIV_NodeBar = document.createElement("DIV");
	this.DIV_NodeBar.className = "nodeBar"; 
	
	this.DIV_NodeLevel = document.createElement("DIV");
	this.DIV_NodeLevel.className = "nodeLevel";	
	
	this.DIV_NodeFlag = document.createElement("DIV");
	this.DIV_NodeFlag.className = "nodeFlag";	
	this.DIV_NodeFlag.id = "nf" + this.NodeId;	
		
	this.DIV_NodeImg = document.createElement("DIV");	
	this.DIV_NodeImg.className = "nodeImg";
	if((this.NodeImg != null) && (this.NodeImg.length > 0)){		
		var img = document.createElement("IMG");
		img.src = this.NodeImg;
		img.style.width = "16px";
		img.style.height = "16px";
		this.DIV_NodeImg.appendChild(img);
	} 
	
	//checkbox
	this.DIV_Checkbox = document.createElement("DIV");	
	
	//text
	this.DIV_NodeTxT = document.createElement("DIV");
	this.DIV_NodeTxT.className = "nodeTxt";	
	if((this.NodeUrl == null) || (this.NodeUrl.length < 1)){	
		this.DIV_NodeTxT.appendChild(document.createTextNode(this.NodeTxt));	
	}else{
		var A = document.createElement("A");
		A.target = this.NodeTarget;
		A.href = this.NodeUrl;
		A.onfocus = function(){
		    this.blur();
		}		
		A.appendChild(document.createTextNode(this.NodeTxt));	
		this.DIV_NodeTxT.appendChild(A);	
	}

	//--------------------
	this.DIV_NodeBar.appendChild(this.DIV_NodeLevel);
	this.DIV_NodeBar.appendChild(this.DIV_NodeFlag);
	this.DIV_NodeBar.appendChild(this.DIV_NodeImg);
	this.DIV_NodeBar.appendChild(this.DIV_Checkbox);
	this.DIV_NodeBar.appendChild(this.DIV_NodeTxT);
}

function MyTree(treeId){
	this.TreeBar = document.getElementById(treeId);
	if(this.TreeBar == null) 
	{
		this.TreeBar = document.createElement("DIV");
		document.documentElement.appendChild(this.TreeBar);
	}
	
	this.TreeBar.className = "treebar";	

	this.IsCheckbox = (arguments.length > 1) ? arguments[1] : false;
 	this.NodePId = (arguments.length > 2) ? arguments[2] : 0;	
	this.Nodes = new Array();			
	this.Created = false;
	this.SelectedNode = null;
	 
	return this;
}

MyTree.prototype.Add = function(nodeId, nodePId, nodeTxt, nodeUrl, nodeTarget, nodeImg){
	var node = new MyTreeNode(nodeId, nodePId, nodeTxt, nodeUrl, nodeTarget, nodeImg); 
	this.Nodes.push(node);
	
	var tree = this;
	node.DIV_NodeBar.onmouseover = function(){
		if(tree.SelectedNode == this) return false;
		this.className = "nodeBarOver";
	}
	node.DIV_NodeBar.onmouseout = function(){
		if(tree.SelectedNode == this) return false;
		this.className = "nodeBar";
	}		
	node.DIV_NodeBar.onclick = function(){
		if(tree.SelectedNode == this) return true;
		this.className = "nodeBarSelected";
		if(tree.SelectedNode != null) tree.SelectedNode.className = "nodeBar";
		tree.SelectedNode = this;
	}		
		
	this.Created = false;
}

MyTree.prototype.AddSplitor = function(nodeId, nodePId){
	var node = new Object();	 
	this.Nodes.push(node);
	
    node.NodeId = nodeId;
	node.NodePId = nodePId; 
	node.NodeLevel = 0;
	node.NodeType = 2;
	
	//HTML
	node.DIV_NodeBar = document.createElement("DIV");
	node.DIV_NodeBar.className = "nodeSplitor"; 	
		
	this.Created = false;
}



MyTree.prototype.ShowChildren = function(prtNode){
	var node = null;
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodePId == prtNode.NodeId){
			node.DIV_NodeBar.style.display = "block";
			node.NodeExpanded = false;
			if(node.HasChildren) this.SetNodeFlagStyle(node);
			//this.ShowChildren(node); //
		}
	}
}

MyTree.prototype.HideChildren = function(prtNode){
	var node = null;
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodePId == prtNode.NodeId){
			node.DIV_NodeBar.style.display = "none";
			node.NodeExpanded = false;
			if(node.HasChildren) {
				this.SetNodeFlagStyle(node);
				this.HideChildren(node); //
			}
		}
	}
}

MyTree.prototype.SetNodeFlagStyle = function(node){	
	//MyDebug_WriteLn(node.NodeTxt + " " + node.NodeExpanded);
	if(node.NodeExpanded == true){			 				
		node.DIV_NodeFlag.className = "nodeSub";		
	}
	else{				
		node.DIV_NodeFlag.className = "nodePlus";			
	}	
}

MyTree.prototype.SetCheckbox = function(node){ 
 	switch(node.NodeChecked){
 		case 0: node.DIV_Checkbox.className = "nodeCheck"; break;
 		case 1: node.DIV_Checkbox.className = "nodeChecked"; break;
 		case 2: node.DIV_Checkbox.className = "nodeChecked_part"; break; 
 	} 
}

MyTree.prototype.ChildrenIsChecked = function(prtNode, nd){
	var node = null;
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if((node.NodeId != nd.NodeId) && (node.NodePId == prtNode.NodeId)){
			//MyDebug_WriteLn("Node="+ node.NodeTxt + ", Checked=" + node.NodeChecked);
			if(node.NodeChecked != 1) return false;
			this.ChildrenIsChecked(node, prtNode);
		}
	}
	return true;
}

MyTree.prototype.SetChildrenCheckbox = function(prtNode){
	var node = null;
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodePId == prtNode.NodeId){
			node.NodeChecked = prtNode.NodeChecked;
			this.SetCheckbox(node);
			this.SetChildrenCheckbox(node);
		}
	}
}

MyTree.prototype.SetParentCheckbox = function(node){
	var prtNode = null;
	var nodeChecked = false;
	
	//MyDebug_WriteLn("NodeChecked = " + node.NodeChecked);
	
	for(var i = 0; i < this.Nodes.length; i++){
		prtNode = this.Nodes[i];
		if(prtNode.NodeId == node.NodePId){
			nodeChecked = this.ChildrenIsChecked(prtNode, node);			
		 
		 //MyDebug_WriteLn("ChildrenIsChecked = " + nodeChecked + " ParentChecked = " + prtNode.NodeChecked);
			if(nodeChecked){
				prtNode.NodeChecked = (node.NodeChecked == 1) ? 1 : 2;				
			}else{
				prtNode.NodeChecked = (node.NodeChecked == 1) ? 2 : node.NodeChecked;
			}			
			this.SetCheckbox(prtNode);			
			this.SetParentCheckbox(prtNode);
		}
	}
}

MyTree.prototype.SetNodeStyle = function(node){	
	//MyDebug_WriteLn(node.NodeTxt + " = " + node.HasChildren);
    
    if(node.NodeType != 1) return;
 
	node.DIV_NodeBar.className = "nodeBar";
	node.DIV_NodeBar.style.display = "block";		
	node.DIV_NodeFlag.className = "nodeSub";	 
	node.NodeExpanded = true;
	
	var tree = this;
	if(node.HasChildren == true){		
		node.DIV_NodeFlag.onclick = function(){	
			node.NodeExpanded = !node.NodeExpanded;
			tree.SetNodeFlagStyle(node);
			if(node.NodeExpanded){
				tree.ShowChildren(node);				
			}else{
				tree.HideChildren(node);	
			}				 				
		}
	}
	else{				
		node.DIV_NodeFlag.className = "nodeFlag";				
	}
	
	//是CHECKBOX
	if(this.IsCheckbox){ 		
		node.DIV_Checkbox.className = "nodeCheck";
		node.DIV_Checkbox.style.display = "block";			
		node.DIV_Checkbox.onclick = function(){						
			node.NodeChecked = (node.NodeChecked == 0) ? 1 : 0;
			tree.CheckedValues = "";
			tree.SetCheckbox(node);
			tree.SetChildrenCheckbox(node);
			tree.SetParentCheckbox(node);					 			
		}
	}else{				 
		node.DIV_Checkbox.style.display = "none";			
	}	
}

MyTree.prototype.AddChildren = function(nodePId, nPrtLevel){
	var node = null;
	var nChildNum = 0;
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodePId == nodePId){
		    if(node.NodeType == 1){
			    node.NodeLevel = nPrtLevel + 1;
			    node.DIV_NodeLevel.style.width = (node.NodeLevel * TREENODE_LEVEL_WIDTH) + "px";		
			}		
			this.TreeBar.appendChild(node.DIV_NodeBar);
			
			nChildNum++;
			node.HasChildren = this.AddChildren(node.NodeId, node.NodeLevel) > 0;			 
			this.SetNodeStyle(node);
		}
	}
	return nChildNum;
}

MyTree.prototype.Create = function(){
	MyClearChildren(this.TreeBar);
	
	
	this.TreeBar.style.display = "block";		
	
	this.AddChildren(this.NodePId, -1);	
} 

MyTree.prototype.Show = function(){
	if(this.TreeBar == null) return false;		
	if(this.Created == false) this.Create();	
}

MyTree.prototype.ShowAll = function(prtNode){
	var node = null;
	this.SetNodeFlagStyle(prtNode); 
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodePId == prtNode.NodeId){
			node.NodeExpanded = prtNode.NodeExpanded;
			node.DIV_NodeBar.style.display = node.NodeExpanded ? "block" : "none";	
			//alert(node.NodeTxt);
			if(node.HasChildren) {
				this.SetNodeFlagStyle(node);
				this.ShowAll(node);
			}
		}
	}
}

MyTree.prototype.FullExpand = function(){
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodePId == this.NodePId){
			node.NodeExpanded = true;				
			if(node.HasChildren) this.ShowAll(node);	 				
		}
	}
}

MyTree.prototype.FullCollapse = function(){
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodePId == this.NodePId){
			node.NodeExpanded = false;	 
			if(node.HasChildren) this.ShowAll(node);	 
		}
	}
}

MyTree.prototype.GetCheckedValues = function(){
	if(this.IsCheckbox == false) return "";
	var node = null;
	var str = "";
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodeChecked != 0){
			str += ("," + node.NodeId);
		}
	}
	
	if(str.length > 0) str = str.substring(1,str.length);
	return str;	
}

MyTree.prototype.CheckedAll = function(chked){
	if(this.IsCheckbox == false) return false;
	var node = null;
 
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		node.NodeChecked = chked ? 1 : 0;
		this.SetCheckbox(node);
	}	
 
	return true;		
}

MyTree.prototype.GetNode = function(nodeId){
	var node = null;
	for(var i = 0; i < this.Nodes.length; i++){
		node = this.Nodes[i];
		if(node.NodeId == nodeId) return node;
	}		
	return null;	
}

MyTree.prototype.SetCheckedNodes = function(nodeIds){

    this.CheckedAll(false);
    
	if((nodeIds==null) && (nodeIds.length < 1)) return false;
	var ndIds = nodeIds.split(",");
	if(ndIds == null) return false;	
	
	var node = null;
	for(var i = 0; i < ndIds.length; i++){
		node = this.GetNode(ndIds[i]);		 
		if(node != null){
			node.NodeChecked = 1;
			this.SetCheckbox(node);
		}
	}
}


CSS源码:

.treebar{	
	margin: 0px;
	padding: 0px;
	border-style: solid;
	border-width: 1px;
	border-color: #cccccc;
	
	font-family: 宋体;
	font-size: 12px;	
}

.nodeBar{	
	clear: both;
	height: 20px;
	background: #ffffff;	
}

.nodeSplitor{
	clear: both;
	display: block;	
	width: 100%;
	height: 1px;
	margin-top: 1px;
	margin-bottom: 3px;
	
	border-bottom-style: solid;
	border-bottom-width: 1px;
	border-bottom-color: #cccccc;
}

.nodeBarSelected{
	clear: both;
	height: 20px;
	background: #ffc06f; /*url(itemover.png) repeat-x;	*/	
}

.nodeBarOver{
	clear: both;
	height: 20px;
	background: url(itemover.png) repeat-x;				
	
	
}

.nodeFlag{
	float: left;
	width: 12px; 
	height: 100%;
		
/*	
	border-style: solid;
	border-width: 1px;
	border-color: red;
	 */	
}

.nodePlus{
	float: left;
	width: 12px; 
	height: 100%; 
	background: url(plus.png) 2px 7px no-repeat;
	cursor: pointer;
	
	/*
	border-style: solid;
	border-width: 1px;
	border-color: red;
	 */		
}

.nodeSub{
	float: left;
	width: 12px; 
	height: 100%; 
	background: url(sub.png) 2px 7px no-repeat;
	cursor: pointer;
	
	/*
	border-style: solid;
	border-width: 1px;
	border-color: red;
	 */		
}

.nodeLevel{
	float: left;
	height: 100%;
	width: 6px;
	
	/*
	border-style: solid;
	border-width: 1px;
	border-color: red;
	 */
} 

.nodeImg{
	float: left;	
	width: 18px;	
	padding-left: 3px;
	padding-top: 3px; 	
	
	/*
	border-style: solid;
	border-width: 1px;
	border-color: red;
	 */	
} 

/*------------------ Checkbox style -------------------------------- */
.nodeCheck{
	float: left;
	width: 20px;
	height: 100%;
	background: url(check_no.png) 0px 3px no-repeat;
}

.nodeChecked{
	float: left;
	width: 20px;
	height: 100%;
	background: url(checked.png) 0px 3px no-repeat;
}

.nodeChecked_part{
	float: left;
	width: 20px;
	height: 100%;
	background: url(checked_part.png) 0px 3px no-repeat;
}

/*------------------ NodeText style -------------------------------- */
.nodeTxt{
	float: left; 
	height: 22px; 
	color: #000000;
	padding-top: 2px;
	
	/*
	border-style: solid;
	border-width: 1px;
	border-color: red;
	 */	
}

.nodeTxt A{
	color: #004276;
	text-decoration: none;
	
	/*
	border-style: solid;
	border-width: 1px;
	border-color: red;
	 */	
}

.nodeTxt A:hover 
{
	text-decoration: underline;
	color: #ba2636;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值