» Home
  » Introduction
 

Database Driven Multi-Level Tree Control Demo!


Click here to view the demo
downloadcode.gif (1195 bytes)

<p>Enjoy, this demo is provided Sébastien Cramatte. He took our demo and expanded it so the tree control would allow multi-folders below each root folder + this is still driven from a database. This is
what ASPFree uses as the default navigation Tool on the left side. This
trademark of ASPFree.com has what has made this site unique from a lot of of
other ASP sites. We continue to provide our source at no cost!
. I used this code for the basis of my control. I’ve altered mine to
provide the functionality on my site but here is his demo! Listed are the
4 main files that do all the work. There is also some images are included
with this demo but they are available from the download. Have Fun! Share
your code!</p>
<p> </p>
<p><b>Code for the Multi-Navigation Tool. This Demo has 3 pages, 1
jscript file & and database.</b> <b>Here is the code for 3 page
involved and the JScript file.</b></p>
<p> </p>
<p><b>Page 1 The Default Page. (Its a framed page) Holds all
references to the 2 pages that will load</b></p>
<p><html><br>
<head><br>
<title>JavaScript Tree Control Template</title><br>
</head><br>
<frameset cols="210,*"><br>
<frame src="left2.asp" name="TOC"><br>
<frame src="main.htm" name="basefrm"><br>
</frameset><br>
</html></p>
<p><b>Page 2. The left side of the Framed page also will hold the Tree
Control. This is the real heart of the whole control</b></p>
<p><%@LANGUAGE=VBScript %><br>
<%<br>
Set conn = Server.CreateObject("ADODB.Connection")<br>
Set Rs = Server.CreateObject("ADODB.Recordset")<br>
conn.open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("toc.mdb")<br>
<br>
strsql = "SELECT tblCategory.level, tblCategory.[ID], tblCategory.Sort AS CatSort, tblPages.sort AS LinkSort, tblCategory.[Name] AS CatName, tblCategory.HREF AS CatURL, tblPages.[Name] AS LinkName, tblPages.href AS LinkURL FROM tblCategory LEFT JOIN tblPages ON tblCategory.[ID] = tblPages.[ID] ORDER BY tblCategory.Sort, tblPages.sort"<br>
<br>
rs.open strsql, conn, 2, 2<br>
<br>
if not rs.eof then rs.movefirst<br>
currentID = "" %><br>
<html><br>
<head><br>
<link rel="stylesheet" href="ftie4style.css"><br>
<!– Infrastructure code for the tree –><br>
<script src="ftiens4.js"></script><br>
<!– Execution of the code that actually builds the specific tree –><br>
<script><br>
USETEXTLINKS = 1<br>
<br>
<%<br>
<br>
Do While Not Rs.EOF<br>
    level = Rs("level")<br>
    Select case level<br>
case "0" %><br>
level_0 = gFld("<%= Rs("CatName") %>", "<%= Rs("CatUrl") %>")<br>
    <%Case Else <br>
    parent_level= mid(level,1,len(level)-1) %><br>
    level_<%=level%> = insFld(level_<%=parent_level%>, gFld("<%= Rs("CatName") %>", "<%= Rs("CatUrl") %>"))<br>
        <% currentID = Rs("ID")<br>
        savedID = Rs("ID")<br>
        Do While currentID = savedID and not rs.eof<br>
        if Rs("LinkName") <> "" Then %><br>
        insDoc(level_<%=level%>, gLnk(0, "<%= Rs("LinkName") %>", "<%= Rs("LinkURL") %>"))<br>
<%<br>
End if<br>
Rs.MoveNext<br>
<br>
if not rs.eof then currentID = Rs("ID")<br>
Loop<br>
End Select<br>
<br>
if currentID = "" then rs.movenext<br>
<br>
Loop %><br>
</script><br>
<script><br>
initializeDocument()<br>
</script><br>
<base target="basefrm"><br>
<title></title><br>
</head><br>
<body bgcolor="white"><br>
</body><br>
</html><br>
</p>
<p><b>Page 3. This is the main page. This is where all the pages
that are listed in the Tree Control will be displayed</b></p>
<p><html><br>
<head><title></title></head><br>
<body><br>
<h2>Start Page</h2><br>
</body><br>
</html></p>
<p> </p>
<p><b>The JScript File that processes the Tree Control</b></p>
<p>//**************************************************************** <br>
// You are free to copy the "Folder-Tree" script as long as you <br>
// keep this copyright notice: <br>
// Script found in: http://www.geocities.com/Paris/LeftBank/2178/ <br>
// Author: Marcelino Alves Martins ([email protected]) December ’97. <br>
//**************************************************************** <br>
<br>
//Log of changes: <br>
// 17 Feb 98 – Fix initialization flashing problem with Netscape<br>
// <br>
// 27 Jan 98 – Root folder starts open; support for USETEXTLINKS; <br>
// make the ftien4 a js file <br>
// <br>
<br>
<br>
// Definition of class Folder <br>
// ***************************************************************** <br>
<br>
function Folder(folderDescription, hreference) //constructor <br>
{ <br>
//constant data <br>
this.desc = folderDescription <br>
this.hreference = hreference <br>
this.id = -1 <br>
this.navObj = 0 <br>
this.iconImg = 0 <br>
this.nodeImg = 0 <br>
this.isLastNode = 0 <br>
<br>
//dynamic data <br>
this.isOpen = true <br>
this.iconSrc = "ftv2folderopen.gif" <br>
this.children = new Array <br>
this.nChildren = 0 <br>
<br>
//methods <br>
this.initialize = initializeFolder <br>
this.setState = setStateFolder <br>
this.addChild = addChild <br>
this.createIndex = createEntryIndex <br>
this.hide = hideFolder <br>
this.display = display <br>
this.renderOb = drawFolder <br>
this.totalHeight = totalHeight <br>
this.subEntries = folderSubEntries <br>
this.outputLink = outputFolderLink <br>
} <br>
<br>
function setStateFolder(isOpen) <br>
{ <br>
var subEntries <br>
var totalHeight <br>
var fIt = 0 <br>
var i=0 <br>
<br>
if (isOpen == this.isOpen) <br>
return <br>
<br>
if (browserVersion == 2) <br>
{ <br>
totalHeight = 0 <br>
for (i=0; i < this.nChildren; i++) <br>
totalHeight = totalHeight + this.children[i].navObj.clip.height <br>
subEntries = this.subEntries() <br>
if (this.isOpen) <br>
totalHeight = 0 – totalHeight <br>
for (fIt = this.id + subEntries + 1; fIt < nEntries; fIt++) <br>
indexOfEntries[fIt].navObj.moveBy(0, totalHeight) <br>
} <br>
this.isOpen = isOpen <br>
propagateChangesInState(this) <br>
} <br>
<br>
function propagateChangesInState(folder) <br>
{ <br>
var i=0 <br>
<br>
if (folder.isOpen) <br>
{ <br>
if (folder.nodeImg) <br>
if (folder.isLastNode) <br>
folder.nodeImg.src = "ftv2mlastnode.gif" <br>
else <br>
    folder.nodeImg.src = "ftv2mnode.gif" <br>
folder.iconImg.src = "ftv2folderopen.gif" <br>
for (i=0; i<folder.nChildren; i++) <br>
folder.children[i].display() <br>
} <br>
else <br>
{ <br>
if (folder.nodeImg) <br>
if (folder.isLastNode) <br>
folder.nodeImg.src = "ftv2plastnode.gif" <br>
else <br>
    folder.nodeImg.src = "ftv2pnode.gif" <br>
folder.iconImg.src = "ftv2folderclosed.gif" <br>
for (i=0; i<folder.nChildren; i++) <br>
folder.children[i].hide() <br>
} <br>
} <br>
<br>
function hideFolder() <br>
{ <br>
if (browserVersion == 1) { <br>
if (this.navObj.style.display == "none") <br>
return <br>
this.navObj.style.display = "none" <br>
} else { <br>
if (this.navObj.visibility == "hiden") <br>
return <br>
this.navObj.visibility = "hiden" <br>
} <br>
<br>
this.setState(0) <br>
} <br>
<br>
function initializeFolder(level, lastNode, leftSide) <br>
{ <br>
var j=0 <br>
var i=0 <br>
var numberOfFolders <br>
var numberOfDocs <br>
var nc <br>
<br>
nc = this.nChildren <br>
<br>
this.createIndex() <br>
<br>
var auxEv = "" <br>
<br>
if (browserVersion > 0) <br>
auxEv = "<a href=’javascript:clickOnNode("+this.id+")’>" <br>
else <br>
auxEv = "<a>" <br>
<br>
if (level>0) <br>
if (lastNode) //the last ‘brother’ in the children array <br>
{ <br>
this.renderOb(leftSide + auxEv + "<img name=’nodeIcon" + this.id + "’ src=’ftv2mlastnode.gif’ width=16 height=22 border=0></a>") <br>
leftSide = leftSide + "<img src=’ftv2blank.gif’ width=16 height=22>" <br>
this.isLastNode = 1 <br>
} <br>
else <br>
{ <br>
this.renderOb(leftSide + auxEv + "<img name=’nodeIcon" + this.id + "’ src=’ftv2mnode.gif’ width=16 height=22 border=0></a>") <br>
leftSide = leftSide + "<img src=’ftv2vertline.gif’ width=16 height=22>" <br>
this.isLastNode = 0 <br>
} <br>
else <br>
this.renderOb("") <br>
<br>
if (nc > 0) <br>
{ <br>
level = level + 1 <br>
for (i=0 ; i < this.nChildren; i++) <br>
{ <br>
if (i == this.nChildren-1) <br>
this.children[i].initialize(level, 1, leftSide) <br>
else <br>
this.children[i].initialize(level, 0, leftSide) <br>
} <br>
} <br>
} <br>
<br>
function drawFolder(leftSide) <br>
{ <br>
if (browserVersion == 2) { <br>
if (!doc.yPos) <br>
doc.yPos=8 <br>
doc.write("<layer id=’folder" + this.id + "’ top=" + doc.yPos + " visibility=hiden>") <br>
} <br>
<br>
doc.write("<table ") <br>
if (browserVersion == 1) <br>
doc.write(" id=’folder" + this.id + "’ style=’position:block;’ ") <br>
doc.write(" border=0 cellspacing=0 cellpadding=0>") <br>
doc.write("<tr><td>") <br>
doc.write(leftSide) <br>
this.outputLink() <br>
doc.write("<img name=’folderIcon" + this.id + "’ ") <br>
doc.write("src=’" + this.iconSrc+"’ border=0></a>") <br>
doc.write("</td><td valign=middle nowrap>") <br>
if (USETEXTLINKS) <br>
{ <br>
this.outputLink() <br>
doc.write(this.desc + "</a>") <br>
} <br>
else <br>
doc.write(this.desc) <br>
doc.write("</td>") <br>
doc.write("</table>") <br>
<br>
if (browserVersion == 2) { <br>
doc.write("</layer>") <br>
} <br>
<br>
if (browserVersion == 1) { <br>
this.navObj = doc.all["folder"+this.id] <br>
this.iconImg = doc.all["folderIcon"+this.id] <br>
this.nodeImg = doc.all["nodeIcon"+this.id] <br>
} else if (browserVersion == 2) { <br>
this.navObj = doc.layers["folder"+this.id] <br>
this.iconImg = this.navObj.document.images["folderIcon"+this.id] <br>
this.nodeImg = this.navObj.document.images["nodeIcon"+this.id] <br>
doc.yPos=doc.yPos+this.navObj.clip.height <br>
} <br>
} <br>
<br>
function outputFolderLink() <br>
{ <br>
if (this.hreference) <br>
{ <br>
doc.write("<a href=’" + this.hreference + "’ TARGET="basefrm" ") <br>
if (browserVersion > 0) <br>
doc.write("onClick=’javascript:clickOnFolder("https://iislogs.com")’") <br>
doc.write(">") <br>
} <br>
else <br>
doc.write("<a>") <br>
// doc.write("<a href=’javascript:clickOnFolder("+this.id+")’>") <br>
} <br>
<br>
function addChild(childNode) <br>
{ <br>
this.children[this.nChildren] = childNode <br>
this.nChildren++ <br>
return childNode <br>
} <br>
<br>
function folderSubEntries() <br>
{ <br>
var i = 0 <br>
var se = this.nChildren <br>
<br>
for (i=0; i < this.nChildren; i++){ <br>
if (this.children[i].children) //is a folder <br>
se = se + this.children[i].subEntries() <br>
} <br>
<br>
return se <br>
} <br>
<br>
<br>
// Definition of class Item (a document or link inside a Folder) <br>
// ************************************************************* <br>
<br>
function Item(itemDescription, itemLink) // Constructor <br>
{ <br>
// constant data <br>
this.desc = itemDescription <br>
this.link = itemLink <br>
this.id = -1 //initialized in initalize() <br>
this.navObj = 0 //initialized in render() <br>
this.iconImg = 0 //initialized in render() <br>
this.iconSrc = "ftv2doc.gif" <br>
<br>
// methods <br>
this.initialize = initializeItem <br>
this.createIndex = createEntryIndex <br>
this.hide = hideItem <br>
this.display = display <br>
this.renderOb = drawItem <br>
this.totalHeight = totalHeight <br>
} <br>
<br>
function hideItem() <br>
{ <br>
if (browserVersion == 1) { <br>
if (this.navObj.style.display == "none") <br>
return <br>
this.navObj.style.display = "none" <br>
} else { <br>
if (this.navObj.visibility == "hiden") <br>
return <br>
this.navObj.visibility = "hiden" <br>
} <br>
} <br>
<br>
function initializeItem(level, lastNode, leftSide) <br>
{ <br>
this.createIndex() <br>
<br>
if (level>0) <br>
if (lastNode) //the last ‘brother’ in the children array <br>
{ <br>
this.renderOb(leftSide + "<img src=’ftv2lastnode.gif’ width=16 height=22>") <br>
leftSide = leftSide + "<img src=’ftv2blank.gif’ width=16 height=22>" <br>
} <br>
else <br>
{ <br>
this.renderOb(leftSide + "<img src=’ftv2node.gif’ width=16 height=22>") <br>
leftSide = leftSide + "<img src=’ftv2vertline.gif’ width=16 height=22>" <br>
} <br>
else <br>
this.renderOb("") <br>
} <br>
<br>
function drawItem(leftSide) <br>
{ <br>
if (browserVersion == 2) <br>
doc.write("<layer id=’item" + this.id + "’ top=" + doc.yPos + " visibility=hiden>") <br>
<br>
doc.write("<table ") <br>
if (browserVersion == 1) <br>
doc.write(" id=’item" + this.id + "’ style=’position:block;’ ") <br>
doc.write(" border=0 cellspacing=0 cellpadding=0>") <br>
doc.write("<tr><td>") <br>
doc.write(leftSide) <br>
doc.write("<a href=" + this.link + ">") <br>
doc.write("<img id=’itemIcon"+this.id+"’ ") <br>
doc.write("src=’"+this.iconSrc+"’ border=0>") <br>
doc.write("</a>") <br>
doc.write("</td><td valign=middle nowrap>") <br>
if (USETEXTLINKS) <br>
doc.write("<a href=" + this.link + ">" + this.desc + "</a>") <br>
else <br>
doc.write(this.desc) <br>
doc.write("</table>") <br>
<br>
if (browserVersion == 2) <br>
doc.write("</layer>") <br>
<br>
if (browserVersion == 1) { <br>
this.navObj = doc.all["item"+this.id] <br>
this.iconImg = doc.all["itemIcon"+this.id] <br>
} else if (browserVersion == 2) { <br>
this.navObj = doc.layers["item"+this.id] <br>
this.iconImg = this.navObj.document.images["itemIcon"+this.id] <br>
doc.yPos=doc.yPos+this.navObj.clip.height <br>
} <br>
} <br>
<br>
<br>
// Methods common to both objects (pseudo-inheritance) <br>
// ******************************************************** <br>
<br>
function display() <br>
{ <br>
if (browserVersion == 1) <br>
this.navObj.style.display = "block" <br>
else <br>
this.navObj.visibility = "show" <br>
} <br>
<br>
function createEntryIndex() <br>
{ <br>
this.id = nEntries <br>
indexOfEntries[nEntries] = this <br>
nEntries++ <br>
} <br>
<br>
// total height of subEntries open <br>
function totalHeight() //used with browserVersion == 2 <br>
{ <br>
var h = this.navObj.clip.height <br>
var i = 0 <br>
<br>
if (this.isOpen) //is a folder and _is_ open <br>
for (i=0 ; i < this.nChildren; i++) <br>
h = h + this.children[i].totalHeight() <br>
<br>
return h <br>
} <br>
<br>
<br>
// Events <br>
// ********************************************************* <br>
<br>
function clickOnFolder(folderId) <br>
{ <br>
var clicked = indexOfEntries[folderId] <br>
<br>
if (!clicked.isOpen) <br>
clickOnNode(folderId) <br>
<br>
return <br>
<br>
if (clicked.isSelected) <br>
return <br>
} <br>
<br>
function clickOnNode(folderId) <br>
{ <br>
var clickedFolder = 0 <br>
var state = 0 <br>
<br>
clickedFolder = indexOfEntries[folderId] <br>
state = clickedFolder.isOpen <br>
<br>
clickedFolder.setState(!state) //open<->close <br>
} <br>
<br>
function initializeDocument() <br>
{ <br>
if (doc.all) <br>
browserVersion = 1 //IE4 <br>
else <br>
if (doc.layers) <br>
browserVersion = 2 //NS4 <br>
else <br>
browserVersion = 0 //other <br>
<br>
level_0.initialize(0, 1, "") <br>
level_0.display()<br>
<br>
if (browserVersion > 0) <br>
{ <br>
doc.write("<layer top="+indexOfEntries[nEntries-1].navObj.top+"> </layer>") <br>
<br>
// close the whole tree <br>
clickOnNode(0) <br>
// open the root folder <br>
clickOnNode(0) <br>
} <br>
} <br>
<br>
// Auxiliary Functions for Folder-Treee backward compatibility <br>
// ********************************************************* <br>
<br>
function gFld(description, hreference) <br>
{ <br>
folder = new Folder(description, hreference) <br>
return folder <br>
} <br>
<br>
function gLnk(target, description, linkData) <br>
{ <br>
fullLink = "" <br>
<br>
if (target==0) <br>
{ <br>
fullLink = "’"+linkData+"’ target="basefrm"" <br>
} <br>
else <br>
{ <br>
if (target==1) <br>
fullLink = "’http://"+linkData+"’ target=_blank" <br>
else <br>
fullLink = "’http://"+linkData+"’ target="basefrm"" <br>
} <br>
<br>
linkItem = new Item(description, fullLink) <br>
return linkItem <br>
} <br>
<br>
function insFld(parentFolder, childFolder) <br>
{ <br>
return parentFolder.addChild(childFolder) <br>
} <br>
<br>
function insDoc(parentFolder, document) <br>
{ <br>
parentFolder.addChild(document) <br>
} <br>
<br>
// Global variables <br>
// **************** <br>
<br>
USETEXTLINKS = 0 <br>
indexOfEntries = new Array <br>
nEntries = 0 <br>
doc = document <br>
browserVersion = 0 <br>
selectedFolder=0<br>
</p>