Solved

ASP folder treeview - links to items

Posted on 2009-07-07
3
341 Views
Last Modified: 2012-05-07
Hi,

I am trying to create a treeview of the folders, their subfolders, and files in a specific network share. I have accomplished this so far by getting some open source code off the net. The tree is loading fine; though, I WOULD LIKE THE FILES SHOWN TO HAVE DIRECT LINKS TO THEM. This means that you would be able to click on it in the tree. Can someone please help? (look at the attached code)
<%
Option Explicit
 
Class Collection
	Private m_next,m_len
	Private m_dic	
	
	Public Sub Add(Item)
		m_dic.Add "K" & m_next,Item
		m_next = m_next+1		
		m_len = m_len+1		
	End Sub
	
	Public Sub Clear
		m_dic.RemoveAll 
	End Sub
	
	Public Function Length
		Length=m_len
	End Function
	
	Public Default Function Item(Index)
		Dim tempItem,i
		For Each tempItem In m_dic.Items 
			If i=Index Then
				Set Item=tempItem
				Exit Function
			End If
			i=i+1
		Next	
	End Function
	
	Public Sub Remove(ByVal Index)
		Dim Item,i
		For Each Item In m_dic.Items 
			If i=Index Then
				m_dic.Remove(Item)
				m_len=m_len-1
				Exit Sub
			End If
			i=i+1
		Next			
	End Sub
	
	Private Sub Class_Initialize
		m_len=0
		Set m_dic = Server.CreateObject("Scripting.Dictionary")				
	End Sub
	
	Private Sub Class_Terminate
		Set m_dic = Nothing				
	End Sub
End Class
 
Class Node	
	'Public Parent
	Public Text
	Public Href
	Public Target
	Public ToolTipText
	Public ChildNodes
	Public ImageUrl
	Public ID
	
	Public Sub Init(strText,strHref,strToolTipText)
		Text=strText
		Href=strHref
		ToolTipText=strToolTipText
	End Sub
	Public Sub Add(objNode)
		ChildNodes.Add(objNode)
	End Sub
	
	Private Sub Class_Initialize
		Set ChildNodes = New Collection				
	End Sub
	
	Private Sub Class_Terminate
		Set ChildNodes = Nothing				
	End Sub
End Class
 
Class TreeView
	
	Private m_folder
	Public Color	
	Public Nodes
	Public DefaultTarget
	Public ID
	
	Public Property Let ImagesFolder(strFolder)
		m_folder=strFolder
	End Property
	Public Property Get ImagesFolder()
		ImagesFolder=m_folder	
	End Property
	
	Private Sub Class_Initialize
		Set Nodes = New Collection	
		Color="Navy"
		m_folder="images"					
	End Sub
	
	Private Sub Class_Terminate
		Set Nodes = Nothing				
	End Sub
	
	Public Function AddNode(Text)
		Dim tn 
		Set tn = new Node
		tn.Text=Text
		Nodes.Add(tn)
	End Function
	
	Public Function CreateNode(Text,Href,ToolTipText)
		Dim tn 
		Set tn = new Node
		Call tn.Init(Text,Href,ToolTipText)
		Set CreateNode=tn
	End Function
	Public Function CreateSimpleNode(Text)
		Dim tn 
		Set tn = new Node
		tn.Text = Text
		Set CreateSimpleNode=tn
	End Function
 
	
	Private Sub LoopThru(NodeList,Parent)	
		Dim i,j,Node,blnHasChild,strStyle
		
		If Parent<>"0" Then
			Out ("<ul class=tree id=""N" & Parent & """>")
		Else
			Out ("<ul xstyle='margin-left:20px;' id=""N" & Parent & """>")
		End If
		For i=0 To NodeList.Length-1
			Set Node = NodeList(i)		
			If (Node.ChildNodes.Length>0) Then 
				blnHasChild=True	
			Else
				blnHasChild=False
			End If
			If Node.ImageUrl="" Then
				strStyle=""
			Else
				strStyle="style='list-style-image: url("& Node.ImageUrl &");'"
			End If				
			If blnHasChild Then
				Out("<li "& strStyle &" class=folder id=""P" & Parent & i & """><a class=treeview href=""javascript:toggle"& id &"('N" & Parent & "_" & i & "','P" & Parent & i & "')"">" & Node.Text & "</a>")
			Else
				If Node.Target="" Then
					Node.Target=DefaultTarget
				End If
				Out("<li "& strStyle &" class=file><a class=treeview href=""" & Node.Href & """ target=""" & Node.Target & """  title=""" & Node.ToolTipText & """>" & Node.Text & "</a>")
			End If
			
			If blnHasChild Then		
				Call LoopThru(Node.ChildNodes,Parent & "_" & i)
			End If	
					
			Out ("</li>")
		Next
		Out ("</ul>")
	End Sub
 
	Private Sub Out(s)
		Response.Write(s)
	End Sub
	
	Public Sub Display
		Out("<script>function toggle"& id &"(id,p){var myChild = document.getElementById(id);if(myChild.style.display!='block'){myChild.style.display='block';document.getElementById(p).className='folderOpen';}else{myChild.style.display='none';document.getElementById(p).className='folder';}}</script>")
		Out("<style>ul.tree{display:none;margin-left:17px;}li.folder{list-style-image: url("& ImagesFolder &"/plus.gif);}li.folderOpen{list-style-image: url("& ImagesFolder &"/minus.gif);}li.file{list-style-image: url("& ImagesFolder &"/dot.gif);}a.treeview{color:"& Color &";font-family:verdana;font-size:8pt;}a.treeview:link {text-decoration:none;}a.treeview:visited{text-decoration:none;}a.treeview:hover {text-decoration:underline;}</style>")
		Call LoopThru(Nodes,0)		
	End Sub
	
	Public Sub LoadFromDB(strConn,strMenuTable)
		Dim Conn 
		Set Conn = Server.CreateObject("ADODB.Connection")
		Conn.Open strConn
		
		Dim RS,node,parentid,parentNode
		Set RS = Conn.Execute("SELECT * FROM " & strMenuTable & " ORDER BY MenuID,ParentID")
		
		If Not RS.EOF Then
			Do While Not RS.EOF
				parentid=RS("ParentID")
				
				Dim child				 
				Set child = new Node
				Call child.Init(RS("Text"),RS("URL"),RS("ToolTip"))
				child.ID =RS("MenuID")
				
				If parentid=0 then
					Nodes.Add(child)
				Else
					Set parentNode = FindNode(Nodes,ParentID)
					If Not (parentNode is Nothing) Then
						parentNode.Add(child)
					End If
				End If
				RS.MoveNext		
			Loop
			RS.Close
		End If
		Set RS = Nothing
		Conn.Close 
		Set Conn = Nothing		
	End Sub
	
	Private Function FindNode (nodes,ID)
		dim i,tempNode		
		For i=0 To nodes.Length-1
			Set tempNode = nodes(i)
			if tempNode.Id=ID then
				Set FindNode=tempNode
				Exit Function
			Else
				If tempNode.ChildNodes.length>0 Then
					Set tempNode = FindNode(tempNode.ChildNodes,ID)
					If Not (tempNode is Nothing) Then
						Set FindNode=tempNode
						Exit Function
					End If
				end if
			End If				
		Next
		Set FindNode = Nothing			
	End Function
	
	Public Sub DisplayFolderContents(ByVal strFolderPath)
		Out("<script>function toggle"& id &"(id,p){var myChild = document.getElementById(id);if(myChild.style.display!='block'){myChild.style.display='block';document.getElementById(p).className='folderOpen';}else{myChild.style.display='none';document.getElementById(p).className='folder';}}</script>")
		Out("<style>ul.tree{display:none;margin-left:17px;}li.folder{list-style-image: url("& ImagesFolder &"/plus.gif);}li.folderOpen{list-style-image: url("& ImagesFolder &"/minus.gif);}li.file{list-style-image: url("& ImagesFolder &"/dot.gif);}a.treeview{color:"& Color &";font-family:verdana;font-size:8pt;}a.treeview:link {text-decoration:none;}a.treeview:visited{text-decoration:none;}a.treeview:hover {text-decoration:underline;}</style>")
 
 
 
 
 
 
 
 
 
		Dim fso 
	  Set fso = Server.CreateObject("Scripting.FileSystemObject")
		If fso.FolderExists(strFolderPath) Then			
			Call ListFolderContents(fso.GetFolder(strFolderPath),0)			
		Else
			Out "<font color=red>Folder <b>'" & strFolder & "'</b> does not exist</font>"
		End If
	
		Set fso = Nothing
	End Sub
	
	Private Sub ListFolderContents(objFolder,Parent)
		Dim objSubFolder, objFile	
		If Parent<>"0" Then
			Out ("<ul class=tree id=""N" & Parent & """>")
		Else
			Out ("<ul xstyle='margin-left:20px;' id=""N" & Parent & """>")
		End If	
		
		dim i
		For Each objSubFolder In objFolder.SubFolders
			Out("<li class=folder id=""P" & Parent & i & """><a class=treeview href=""javascript:toggle"& id &"('N" & Parent & "_" & i & "','P" & Parent & i & "')"">")
			Out objSubFolder.Name & "</a>"			
			Call ListFolderContents(objSubFolder,Parent & "_" & i)
			Out "</li>"
			i=i+1
		Next
		
		For Each objFile In objFolder.Files
			Out "<li class=file>" & objFile.Name & "</li>"
		Next
		
		Out "</ul>"
		
		Set objFile = Nothing
		Set objSubFolder = Nothing
		
	End Sub
End Class
 
 
 
 
%>
 
<!--
 
	
	Public Sub LoadFromDB(strConn,strMenuTable)
		Dim Conn 
		Set Conn = Server.CreateObject("ADODB.Connection")
		Conn.Open strConn
		
		
		Call AddNodesFromDB(Conn,strMenuTable,0,-1)
		
		Conn.Close 
		Set Conn = Nothing		
	End Sub
	
	Private Sub AddNodesFromDB(objConn,strTable,intParentId,intDepth)
		Dim RS,node
		Set RS = objConn.Execute("SELECT * FROM " & strTable & " WHERE ParentID=" & intParentId)
		intDepth=intDepth+1
		If Not RS.EOF Then
			Do While Not RS.EOF
				node
				'Call AddNodesFromDB(objConn,strTable,RS("MenuID"),intDepth)
				RS.MoveNext
			Loop
			RS.Close
		End If
		Set RS = Nothing
	End Sub
	-->

Open in new window

0
Comment
Question by:alateos
  • 2
3 Comments
 

Author Comment

by:alateos
ID: 24798144
I think the change needs to be done here:

For Each objFile In objFolder.Files
                  Out "<li class=file>" & objFile.Name & "</li>"
            Next
0
 
LVL 9

Expert Comment

by:rg20
ID: 24798476

to elaborate a bit

you should be able to extract the path from the objfolder object

For Each objFile In objFolder.Files
        <a href="file:///extract path here">filename</a>
next
0
 

Accepted Solution

by:
alateos earned 0 total points
ID: 24798500
Actually, I used filename.shortpath and it worked
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now