Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Troubleshooting the Simple Ajax Treeview

Posted on 2009-06-30
2
669 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:GMMC_man
2 Comments
 
LVL 29

Accepted Solution

by:
Badotz earned 500 total points
ID: 24745864
This is not a question, it is an expression of need for application development, and for those kinds of needs you should hire a developer.

We are not paid to do development here at EE -- we are just volunteers who answer questions for points and the occasional T-shirt.

Perhaps Rent-A-Coder or a similar service can help you.

Best of luck with your project.
0
 

Author Comment

by:GMMC_man
ID: 24746733
Ok - thanks.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question