Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / ASP

ASP TreeList Control (Requires Internet Explorer 5.0+)

Rate me:
Please Sign up or sign in to vote.
4.81/5 (20 votes)
21 Apr 20022 min read 292.4K   3.1K   76  
An ASP class (Classic ASP) that generates a dynamic html tree list control that looks the same as a windows explorer tree but allows multiple columns of data per tree node
treelistctrl = new Array();
treelistctrl_basefolder = null;
treelistctrl_dontrowselect = false;
function treelistctrl_node() {
	this.id = 0;
	this.parentid = 0;
	this.showchildren = false;
	this.haschildren = false;
	this.parentindex = 0;
}
function treelistcontrol_showhide(obj,tlc_id){
	treelistctrl_dontrowselect = true;
	var undefined;
	var temp, temp2, parentid, stack;
	stack = new Array();
	if(treelistctrl[tlc_id] == null) //initialize the array containing info on all elements in the tree
	{
		treelistctrl[tlc_id] = new Array();
		var aobj = obj.parentElement.parentElement.parentElement;
		for (var k=0; k<aobj.children.length; k++)
		{
			treelistctrl[tlc_id][k] = new treelistctrl_node();
			treelistctrl[tlc_id][k].id = aobj.children[k].id;
			temp = treelistctrl[tlc_id][k].id.split('_');
			parentid = treelistctrl[tlc_id][k].id.substr(0, treelistctrl[tlc_id][k].id.length - temp[temp.length-1].length -1);
			if(parentid == temp[0])
				parentid = null;
			treelistctrl[tlc_id][k].parentid = parentid;
			if(k==0) //if we are element 0 or the stack is empty, then we are root level and have no parent index
				treelistctrl[tlc_id][k].parentindex = -1;
			else {
				temp2 = treelistctrl[tlc_id][k-1].id.split('_');
				if(temp.length > temp2.length) { // we are a child of the previous row, put its index on the stack and specify that it has children
					stack[stack.length] = k-1;
				}
				else if(temp.length < temp2.length) // that was the end of the children, we are the next sibling so remove top of stack
					stack.length -= temp2.length-temp.length;
				//otherwise we are a sibling of the previous row, so don't alter the stack
				treelistctrl[tlc_id][k].parentindex = stack[stack.length-1]; //the last item on the stack is the current parentindex
			}
				
			if(k > 0 && stack.length > 0)
			{
				treelistctrl[tlc_id][treelistctrl[tlc_id][k].parentindex].haschildren = true;
				treelistctrl[tlc_id][treelistctrl[tlc_id][k].parentindex].showchildren = aobj.children[k].style.display == 'none' ? false : true;
			}
		}
	}
	var rowcolor1 = '#FFFFFF';
	var rowcolor2 = '#F7F8FA';

	var iexpand1 = "icon_expand_more.gif";
	var iexpand2 = "icon_expand_end.gif";
	var icollapse1 = "icon_collapse_more.gif"
	var icollapse2 = "icon_collapse_end.gif"
	
	var source = obj.src;
	temp = source.split('/');
	var fname = temp[temp.length-1];
	var srcdir = source.substr(0,source.length-fname.length);
	treelistctrl_basefolder = srcdir;
	 
	if(fname==iexpand1 || fname==iexpand2)
	{
		if(fname==iexpand1)
			fname = icollapse1;
		else
			fname = icollapse2;
	}
	else
	{
		if(fname==icollapse1)
			fname = iexpand1;
		else
			fname = iexpand2;
	}
	obj.src = srcdir + fname;
	
	var rowcount = 0;
	var trobj = obj.parentElement.parentElement;
	var tobj = trobj.parentElement;
	var idname = trobj.id;
	var childid, childobj;
	for(var i=0; i<tobj.children.length; i++) {
		childobj = tobj.children[i];
		childid = childobj.id;
		if(idname == childid) //if this is the row that was clicked, toggle its 'showchildren' property
			treelistctrl[tlc_id][i].showchildren = !treelistctrl[tlc_id][i].showchildren;

		//if the parent row has its children showing AND the parent row is visible, set this row to visible else set this row to invisible
		if(treelistctrl[tlc_id][i].parentindex > -1) //elements with no parent are always visible (i.e. make no change)
		{
			if(document.all[treelistctrl[tlc_id][i].parentid].style.display == '') //is parent row style.display visible?
				if(treelistctrl[tlc_id][treelistctrl[tlc_id][i].parentindex].showchildren) //does that row allow its children to be shown?
					childobj.style.display = '';
				else
					childobj.style.display = 'none';
			else
				childobj.style.display = 'none';
		}
					
		if(childobj.style.display=='')
			if(rowcount==0) {
				childobj.bgColor = rowcolor1;
				rowcount = 1;
			} else {
				childobj.bgColor = rowcolor2;
				rowcount = 0;
			}
	}
	
}

function treelistcontrol_rowselect(obj){
	if(treelistctrl_dontrowselect){
		treelistctrl_dontrowselect = false;
		return;
	}
	var x = obj.parentElement;
	var doselect;
	doselect = obj.style.backgroundColor == '#000066' ? false : true;
	for(var i=0; i < x.children.length; i++)
	{
		x.children[i].style.backgroundColor = '';
		x.children[i].style.color = '';
	}
	if(doselect) {
		obj.style.backgroundColor = '#000066';
		obj.style.color = '#FFFFFF';
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
Web application developer, graphic designer, aspiring entrepreneur, snowboarder and electronic music afficianado.


Comments and Discussions