Click here to Skip to main content
15,881,882 members
Articles / Web Development / HTML

DHTML Tree View of Arbitrary Depth using AJAX

Rate me:
Please Sign up or sign in to vote.
4.60/5 (24 votes)
25 Aug 20054 min read 186.9K   2.1K   84  
This article provides a gentle introduction to AJAX by applying that technology to significantly enhance a tree previously rendered using JavaScript.
// Copyright (c)2005 Rewritten Software.  http://www.rewrittensoftware.com
// This script is supplied "as is" without any form of warranty. Rewritten Software 
// shall not be liable for any loss or damage to person or property as a result of using this script.
// Use this script at your own risk!
// You are licensed to use this script free of charge for commercial or non-commercial use providing you do not remove 
// the copyright notice or disclaimer.

///
/// Method to toggle a node in the tree by displaying or hiding relevant elements.
///
function Toggle(item)
{
    
	var div=document.getElementById("D" + item);
	var visible=(div.style.display!="none");
	var key=document.getElementById("P" + item);
	var closed=document.getElementById("C" + item);
	var open=document.getElementById("O" + item);
	
	
	// Check if the item clicked has any children. If it does not then remove the plus/minus icon
	// and replace it with a transaparent gif.
	var removeIcon = div.hasChildNodes() == false;
	
	if( key != null )
	{
		if( !removeIcon )
		{
			if (visible)
			{
		 		// Hide open icon and the node contents
		 		div.style.display="none";
		 		open.style.display="none";
		 		// Show the closed icon
		 		closed.style.display="block";
		 		key.innerHTML="<img src='img/plus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
			}
			else
			{
			    // Show node contents and the icon
		  		div.style.display="block";
		 		open.style.display="block";
		 		// Hide the closed icon
		 		closed.style.display="none";
				key.innerHTML="<img src='img/minus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
			}
		}
		else
			key.innerHTML="<img src='img/transparent.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
	}
}

///
/// Return a XMLHTTPRequest in a browser independent fashion.
///
function GetXMLHttp()
{
    var xmlhttp=false;
    
    try 
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
            xmlhttp = false;
        }
    }

    // Mozilla then?
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
       xmlhttp = new XMLHttpRequest();
    }
    
    return xmlhttp;
}

///
/// Perform an AJAX style request for the contents of a node and put the contents
/// into the empty div.
///
function DelayLoadNode(treeid, item)
{
    var div=document.getElementById("D" + item);
    var xmlhttp = GetXMLHttp();

    // Make sure the node is empty really, and if so fill it
    if (div.innerHTML == "")
    {
        xmlhttp.open("GET", "TreeFill.aspx?tree="+treeid+"&id="+item, true);
        xmlhttp.onreadystatechange=function() 
        {
            if (xmlhttp.readyState==4)
            {
                // DEBUG: alert(xmlhttp.responseText);
                div.innerHTML = xmlhttp.responseText;
                Toggle(item);
            }
        }
        xmlhttp.send(null)
    }
    else // The node is already populated
    {
        Toggle(item);
    }
}

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
Architect Webefinity
Australia Australia
Adrian is current the Solution Architect at CubeBuild.com.

The core of CubeBuild is a website and application platform that is pluggable into ASP.NET MVC. Any MVC application can have content authoring added to its pages with little effort, and new content types are created using IronPython.NET open source components.

We are currently deploying a Point of Service (Web based POS) built on CubeBuild which allows a single web channel for face-to-face sales, and sales through your online store. All from a single inventory base, and from any device.

Comments and Discussions