vbscript版的TreeView,也就是树
'=======================================================
'===efei Super TreeView
'===Code By efei(草不含羞)
'===Mailto:efei731@sina.com
'===2004.7
'=======================================================
Dim tvwRoot
Dim tvwChild
Dim tvwPrevious
Dim tvwNext
tvwRoot = 0
tvwChild = 1
tvwPrevious = 2
tvwNext = 3
'===树的类
Class TreeView
Public Name '对象名
Public TreeView_Name
Private ArrNodes '节点集合,从0开始
Private ArrImage '图标数组
Private AddFromExpand '节点的添加是否来自于父节点的展开
'===一些设置
Public FirstDisplayLevel '初次显示几层
Public UseConnectLine '使用节点之间的连接线
'Public ImagePath '图象目录
Public Target '链接目标
Public Font_Size '文字大小
Public Font_Family '字体
Public Font_Color '颜色
Public SelectedBgcolor '选中的节点背景色
'===图象目录=============================
Private lImagePath
Public Property Get ImagePath()
ImagePath = lImagePath
End Property
Public Property Let ImagePath(ByVal vNewValue)
'更新之前先去掉原来的节点的边框
lImagePath = vNewValue
If Right(lImagePath,1) <> "/" Then lImagePath = lImagePath & "/"
End Property
'========================================
Private lSelectedNode '被选择的节点
Public Property Get SelectedNode()
Set SelectedNode = lSelectedNode
End Property
Public Property Let SelectedNode(ByVal vNewValue)
'更新之前先去掉原来的节点的边框
If Not lSelectedNode Is Nothing Then
With Document.getElementById("txt" & lSelectedNode.Key).style
.background = ""
End With
End If
Set lSelectedNode = vNewValue
With Document.getElementById("txt" & lSelectedNode.Key).style
.background = SelectedBgcolor
End With
End Property
Private lMenuNode
Private lPopupMenu
'===类的析构函数,用于初始化对象==============
Private Sub Class_Initialize()
'===初始化树对象
Document.Write "<table id='TabTreeView' border='0' cellpadding='0' cellspacing='0'></table>"
'===
Redim ArrNodes(0)
Set ArrNodes(0) = Nothing
name=""
TreeView_Name = "Efei Super TreeView"
Set lSelectedNode = Nothing
Set lMenuNode = Nothing
lPopupMenu = ""
Redim ArrImage(1,2)
ArrImage(0,0) = "FolderClose"
ArrImage(1,0) = "FolderClose.gif"
ArrImage(0,1) = "FolderOpen"
ArrImage(1,1) = "FolderOpen.gif"
ArrImage(0,2) = "Rplus"
ArrImage(1,2) = "Rplus.gif"
AddImage "root","root.gif"
AddImage "file","file.gif"
lImagePath = "Image/"
FirstDisplayLevel = 0
UseConnectLine = True
AddFromExpand = False
Target = "_Blank"
Font_Size = "12px"
Font_Family = "宋体"
Font_Color = "#000000"
SelectedBgcolor = "#D4D0C8"
End Sub
'===============================================
'===功能: 添加一个节点
'===参数: Text 节点显示的文本
' Key 节点关键字,该关键字必须唯一
' HyperLink 节点要链接到的地址
' Node 父节点或者兄弟节点
' AddType 添加类型,0-子节点,1-子节点,2-前置节点,3-后置节点
' Image 节点的图片,如空,则使用关闭的文件夹
Public Function add(ByVal Text,ByVal Key,ByVal HyperLink,ByRef Node,ByVal AddType,ByVal Image)
Dim i
'先检查关键字是否已存在,参数是否合法
Key = Trim(Key)
If Key = "" Then
Msgbox "关键字不能为空",vbInformation,TreeView_Name
Exit Function
End If
If NOT Me.Nodes(Key) Is Nothing Then
Msgbox "关键字【" & Key & "】已存在",vbInformation,TreeView_Name
Exit Function
End If
Text = Trim(Text)
If Text = "" Then
Msgbox "节点文本不能为空",vbInformation,TreeView_Name
Exit Function
End If
If IsNumeric(AddType) = True Then
If AddType < 0 And AddType > 3 Then
AddType = 1
End If
Else
AddType = 1
End If
If Node Is Nothing Then
If Me.NodesCount <> 0 Then
Msgbox "根节点只能有一个",vbInformation,TreeView_Name
Exit Function
End If
AddType = 0
End IF
Redim Preserve ArrNodes(Me.NodesCount)
Set ArrNodes(Ubound(ArrNodes)) = New Node
With ArrNodes(Ubound(ArrNodes))
.Key = Key
.Text=Text
.HyperLink=HyperLink
.AddType = AddType
.Image = Image
Set .parentObject = Me
Select Case AddType
Case 0 '根节点
Set .ParentNode=Nothing
Set .NextNode = Nothing
Case 1 '子节点
Set .ParentNode = Node '父节点
If Node.ChildrenCount > 0 Then
Set .PreviousNode = Node.Children(Node.ChildrenCount)
Set Node.Children(Node.ChildrenCount).NextNode = ArrNodes(Ubound(ArrNodes))
End If
Set .NextNode = Nothing
Case 2 '插在前面
Set .ParentNode = Node.ParentNode
Set .PreviousNode = Node.PreviousNode
Set Node.PreviousNode.NextNode = ArrNodes(Ubound(ArrNodes))
Set Node.PreviousNode = ArrNodes(Ubound(ArrNodes))
Set .NextNode = Node
Case 3 '插在后面
Set .ParentNode = Node.ParentNode
Set .PreviousNode = Node
Set .NextNode = Node.NextNode
Set Node.NextNode = ArrNodes(Ubound(ArrNodes))
End Select
If Not .ParentNode Is Nothing Then
'更新父节点的内容
.Level = .ParentNode.Level + 1
.ParentNode.AddChild ArrNodes(Ubound(ArrNodes)) '添加子节点
End If
'If Image <> "" Then
' .Image = Me.Images(Image)
'End If
.Display
If .Level > Me.FirstDisplayLevel Then
.DisplayStatus = 0
Else
If NOT .ParentNode Is Nothing AND AddFromExpand = False Then
If .ParentNode.ExpandStatus = 0 Then
.ParentNode.ExpandStatus = 1 '自己都显示了,那说明父节点肯定是展开的
End If
End If
.DisplayStatus = 1
End If
If NOT .ParentNode Is Nothing Then
RefreshImage .ParentNode
End If
'=================
End With
Set add = ArrNodes(UBound(ArrNodes))
End Function
'==============================================================================================
'===功能: 移除一个节点,包括该节点下的所有子节点=============================================
'===参数: Key 节点的Key,或者是顺序号
Public Function RemoveNode(ByVal Key)
Dim i
Dim Node
Set Node = Nothing
Key = Trim(Key)
If Key = "" Then
Exit Function
End IF
If IsNumeric(Key) = True Then
If Key > 0 And Key <= Me.NodesCount Then
Set Node = Nodes(Key)
End If
Else
Set Node = Nodes(Key)
End If
If Node Is Nothing Then
Exit Function
End If
Do Until Node.ChildrenCount = 0
RemoveNode Node.Children(1).Key
Loop
If Not Node.PreviousNode Is Nothing Then
'如果有前置节点,则要重新设置前置节点的后置节点
Set Node.PreviousNode.NextNode = Node.NextNode
End If
If Not Node.NextNode Is Nothing Then
'如果有后置节点,则要重新设置后置节点的前置节点
Set Node.NextNode.previousNode = Node.PreviousNode
End If
'===设置它的父节点的子节点内容================================
With Node.ParentNode
For i = 0 To .ChildrenCount - 1
If .ChildrenNodes(i).Key = Key Then
Exit For
End If
Next
For i = i To .ChildrenCount - 2
Set .ChildrenNodes(i) = .ChildrenNodes(i+1)
Next
.ChildrenCount = .ChildrenCount - 1
Redim Preserve ChildrenNodes(.ChildrenCount - 1)
End With
'=============================================================
'===设置树对象的节点集合======================================
For i = 0 To Ubound(ArrNodes)
If ArrNodes(i).Key = Key Then Exit For
Next
For i = i To Ubound(ArrNodes) - 1
Set ArrNodes(i) = ArrNodes(i+1)
Next
Redim Preserve ArrNodes(Ubound(ArrNodes)-1)
'==============================================================
Set Node = Document.getElementByID(Key).parentElement.parentElement
Node.ParentElement.parentElement.deleteRow Node.rowIndex
Set Node = Nothing
End Function
'==============================================================================================
'===Nodes集合,返回一个节点,如果没有,返回Nothing========
Public Function Nodes(index)
dim i
Set Nodes = Nothing
If IsNumeric(index) = True Then
If Ubound(ArrNodes)+1 >= index And index > 0 Then
Set Nodes = ArrNodes(index - 1)
End If
Else
If Ubound(ArrNodes) = 0 And ArrNodes(0) Is Nothing Then
Else
For i=0 to Ubound(ArrNodes)
If ArrNodes(i).Key = index Then
Set Nodes = ArrNodes(i)
End If
Next
End If
End If
End Function
'==========================================================
Public Property Get NodesCount()
If Ubound(ArrNodes) = 0 And ArrNodes(0) Is Nothing Then
NodesCount = 0
Else
NodesCount = Ubound(ArrNodes) + 1
End If
End Property
Public Property Let NodesCount(ByVal vNewValue)
Msgbox "NodesCount属性只读!"
End Property
'===功能: 清空图标集合=====================================
Public Function ClearAllImage()
Redim ArrImage(1,0)
End Function
'===End======================================================
'===功能: 添加图标集合=====================================
' 如果关键字imgKey存在,则更新,如不存在,则添加
'===参数: imgKey 图标关键字
' imgSrc 图标的名称
Public Function AddImage(ByVal imgKey,ByVal imgFileName)
dim i
For i=1 To Ubound(ArrImage,2)
If ArrImage(0,i) = imgKey Then
ArrImage(1,i) = imgFileName '更新
Exit Function
End If
Next
'添加
If IsNumeric(imgFileName) = True Then
Msgbox "图标关键字不能为纯数字!请使用字母或字母和数字的组合。"
Exit Function
End If
i = Ubound(ArrImage,2)+1
Redim Preserve ArrImage(1,i)
ArrImage(0,i) = imgKey
ArrImage(1,i) = imgFileName
End Function
'===End======================================================
'===功能: 根据关键字来得到图标的路径=======================
'===参数: imgKey 图标的关键字,或是索引。索引从1开始
Function Images(ByVal imgKey)
Dim i
Images = ""
If imgKey = "" Then Exit Function
If IsNumeric(imgKey) = True Then
If Cint(imgKey) <= Ubound(ArrImage,2) AND Cint(imgKey) > 0 Then
Images = Me.ImagePath & ArrImage(1,imgKey)
End If
Else
For i = 1 To Ubound(ArrImage,2)
If Ucase(ArrImage(0,i)) = Ucase(imgKey) Then
Images = Me.ImagePath & ArrImage(1,i)
Exit For
End If
Next
End If
End Function
'===End=======================================================
'===动态更新各个节点前的连接线符号=============================
Function RefreshImage(ByRef objNode)
Dim i,j
objNode.ExpandStatus = objNode.ExpandStatus
Dim objTemp
If NOT UseConnectLine Then Exit Function
For i = 1 To objNode.ChildrenCount '对子节点进行遍历
Set objTemp = objNode.Children(i)
For j = objNode.Level To 1 Step -1
If NOT objTemp.ParentNode Is Nothing Then
Set objTemp = objTemp.ParentNode
If NOT objTemp.NextNode Is Nothing Then
Document.getElementById(objNode.Children(i).Key).rows(0).cells(j).firstChild.src = Me.ImagePath & "I.gif"
End If
End If
Next
RefreshImage objNode.Children(i)
Next
End Function
'===End===================================
温馨提示:内容为网友见解,仅供参考