Link to home
Start Free TrialLog in
Avatar of GMMC_man
GMMC_manFlag for United States of America

asked on

Troubleshooting the Simple Ajax Treeview

Hi experts,

I downloaded some sample code to create a treeview using ajax and sql but I'm having difficulties getting it to work with my tables. I'm getting the error of a blank page. I downloaded the sample code from http://www.aspwebsolution.com/articles/xmlrep/ajax_treeview1/
I ran the query in the analyzer and it produced good results when i swapped out my userID for intParentId. I can't figure for the life of me where the origional parentId (mentorID) comes from in this script. Please look at the code to help me figure out what I'm doing wrong. Thanks!
treeview.asp
<%
 
Class TreeView
 
Private m_strMenuTable
Private m_strIdField
Private m_strParentIDField
Private m_strTextField
Private m_strConn
 
Public Property Let MenuTable(vNewValue): m_strMenuTable = vNewValue: End Property
Public Property Let IdField(vNewValue): m_strIdField = vNewValue: End Property
Public Property Let ParentIDField(vNewValue): m_strParentIdField = vNewValue: End Property
Public Property Let TextField(vNewValue): m_strTextField = vNewValue: End Property
 
Public Property Let ConnectionString(vNewValue): m_strConn = vNewValue: End Property
 
Private Sub Class_Initialize
	m_strMenuTable = "dbo.Members"
	m_strIdField = "userID"
	m_strParentIDField = "mentorID"
	m_strTextField = "lastName" & "," & "firstName"
End Sub
	
Private Sub Class_Terminate
	'Clear Objects 
End Sub
 
Public Function GetChildNodes(intParentId)
	Dim strSQL
	strSQL = "SELECT " & _
	m_strIdField & "," & _
	m_strTextField & "," & _
	"(SELECT COUNT(*) FROM " & m_strMenuTable & " B WHERE A."& m_strIdField &"=B." & m_strParentIDField & ") AS [HasChild]" & _
	" FROM " & m_strMenuTable &" A "& _
	" WHERE " & m_strParentIDField & " = " & intParentId
	
	Const strXsl = "treeview.xsl"
	GetChildNodes = GetDbXml(m_strConn,strSQL, strXsl)
End Function
 
Private Function GetDbXml(strConn,strSQL,strXsl)
	Dim strXml
	'test strsql
	strXml = GetDbString(strConn,strSql,"__COL","__ROW","__NULL")
	strXml = Replace(strXml,"&","&amp;")
	strXml = Replace(strXml,"<","&lt;")
	strXml = Replace(strXml,"__COL","</col><col>")
	strXml = Replace(strXml,"__ROW","</col></row><row><col>")
	strXml = Replace(strXml,"__NULL","")	
		
	strXml = "<rows><row><col>" & strXml & "</col></row></rows>"			
		
	strXml = Replace(strXml,"<row><col></col></row>","")
	If strXsl <> "" Then		
		Dim xml,xsl
		'Load XML
		if not loadXmlDoc(xml,strXml,"GetDb XML Source") then exit Function
		if not loadXmlDoc(xsl,strXsl,"GetDb XSL Stylesheet") then exit Function
		
		'Transform file
		strXml = xml.transformNode(xsl)
		Set xml = Nothing
		Set xsl = Nothing
	End If
				
	GetDbXml = strXml        	
End Function 
 
Private Function GetDbString(strConn,strSQL, ColumnDelimiter, RowDelimiter, NullExpr)
	Dim Conn 
	Set Conn = Server.CreateObject("ADODB.Connection")
	Conn.Open strConn
			
	Dim RS
	Set RS = Conn.Execute(strSql)		
	If Not RS.EOF Then		
		
		GetDbString = RS.GetString(,, ColumnDelimiter, RowDelimiter, NullExpr) 
 
		'Cleanup!
		RS.Close
	End If	
		
	'Cleanup!		
	Set RS = Nothing
	Conn.Close 
	Set Conn = Nothing					
End function
 
Private Function LoadXmlDoc(ByRef xmldoc, ByVal source, ByVal title)
	If Trim(source) = "" Then
		Exit Function
	End If
	
	Set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
	If Left(source,7) = "http://" Then
		xmldoc.setProperty "ServerHTTPRequest", True
		xmldoc.async =  False
		xmldoc.Load(source)
	Else
		'set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
		xmldoc.async = false
		If InStr(source,"<") Then 
			xmldoc.loadXml(source)
		else
			if InStr(source,":\") = 0 then source = Server.MapPath(source)
			'test source
			xmldoc.load(source)
		end if
	End If
	
	if xmldoc.parseError.errorcode <> 0 then
		'"<error code='" & xmldoc.parseError.errorcode & "'><![CDATA[" & xmldoc.parseError.reason & "]]></error>"
	else
		LoadXmlDoc = True
	end if		
End Function
		
End Class
 
%>
 
treeview.xsl
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" indent="no" omit-xml-declaration="yes" />
	<xsl:variable name="images.folder" select="'images'" />	
	<xsl:template match="/">		
		<div>
			<xsl:apply-templates  />
		</div>		
	</xsl:template>
	<xsl:template match="row">
		<xsl:variable name="menu.id" select="col[1]" />
		<xsl:variable name="text" select="col[2]" />
		<xsl:variable name="has.child" select="col[3]" />
		<table border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td rowspan="2" width="10" valign="top">
					<img id="I{$menu.id}">
						<xsl:choose>
							<xsl:when test="number($has.child) &gt; 0">
								<xsl:attribute name="src">
									<xsl:value-of select="concat($images.folder,'/plus.gif')"/>
								</xsl:attribute>
								<xsl:attribute name="onclick">
									<xsl:value-of select="concat('loadItems(',$menu.id,')')"/>
								</xsl:attribute>
							</xsl:when>
							<xsl:otherwise>
								<xsl:attribute name="src">
									<xsl:value-of select="concat($images.folder,'/dot.gif')"/>
								</xsl:attribute>
							</xsl:otherwise>
						</xsl:choose>
					</img>
				</td>
				<td nowrap="true">
					<a href="javascript:alert({$menu.id});">
						<xsl:value-of select="$text" />						
					</a>
				</td>
			</tr>
			<tr>
				<td id="C{$menu.id}" style="display:none;"></td>
			</tr>
		</table>
	</xsl:template>	
</xsl:stylesheet>
 
ajax.js
// global request and XML document objects
var req;
 
// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {	
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        //isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}
 
// handle onreadystatechange event of req object
function processReqChange(){
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		//alert(req.status)		
		if (req.status == 200) {		
			//alert(req.responseText.substring(req.responseText.indexOf('<div>')))			
			// ...processing statements go here...
			response  = req.responseXML.documentElement;
 
			method =
				response.getElementsByTagName('method')[0].firstChild.data;
			
			//result = response.getElementsByTagName('result')[0].firstChild.data;
			var start = req.responseText.indexOf('<result>') + 8
			var stop = req.responseText.indexOf('</result>')
			result = req.responseText.substring(start,stop);
 
			eval(method + '(\'\', result)');
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}		
 
scripts.js
 
var current_el = null;
var loading;
var began = false      
function getItems(input, response){	
 
	if (response != ''){ 		
	    // Response mode	
	    //alert(response)    
        current_el.innerHTML = response;
        loading.style.display = 'none'
	}else{
		// Input mode			
		var url  = "process_request.asp?p=" + input + "&hash=" + Math.random();
		//alert(url)
		loadXMLDoc(url);		
	}
}
 
function loadItems(id,c){ 
	var img = null;
	if(arguments.length<2){
		c = "C" + id;
		img = document.getElementById("I"+id);
	}
	//alert(c)
	current_el = document.getElementById(c);
	if(current_el.style.display == 'none'){
		current_el.style.display = ''
		if(img)img.src = "images/minus.gif";
	}else{
		current_el.style.display = 'none'
		if(img)img.src = "images/plus.gif";
	}
	if(current_el.innerHTML == ''){
		loading.style.display = ''
		getItems(id,'');		
	}
}
 
function buildTree(id){
	if (!began){
		loading = document.getElementById("Loading")
		document.getElementById(id).style.display = 'none';
		began = true;
	}
	loadItems(0,id)
}
 
process_request.asp
<%Option Explicit%>
<!--#include file="treeview.asp"-->
<%
 
Sub GetItems(parentId)
	Dim strConn,strTable
	 
	strTable = "dbo.Members"
	strConn = "Provider=sqloledb; Data Source=xxx.db.3731644.hostedresource.com; Initial Catalog=xxx; User ID=xxx; Password=xxx"
    
	Dim tv
	Set tv = New TreeView
	tv.MenuTable = strTable
	tv.ConnectionString = strConn
	Response.Write tv.GetChildNodes(MM_userID)
	Set tv = Nothing
	
End Sub
 
 
Dim parentId:parentId = Request("p") 
If parentId="" Then parentId = 0
Response.ContentType = "text/xml"
 
%>
<%= "<?xml version='1.0' encoding='ISO-8859-1'  standalone='yes'?>" %>
<response>
  <method>getItems</method>
  <result><% Call GetItems(parentId) %></result>
</response>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Badotz
Badotz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GMMC_man

ASKER

Ok - thanks.