PHP提供关联数组,但在经典的asp中没有这样的东西。我正在寻找一个类或函数,允许我创建和遍历类似于此的层次结构:
United States
Washington
Electric City
Banks Lake
Lake Chelan
Wapato Point
Gig Harbour
Mexico
Nayarit
Aticama
Asia
India
Mumbai
Pakistan
Sindh
Karachi
正如您所注意到的,层次结构的深度不均匀 - 可能是3,4或5级深度。数据库结构更加复杂,因为有些记录我必须从区域级别开始显示数据,而对于其他记录我应该从国家开始。一组阵列确实会有所帮助,但在ASP中我的双手并列。
答案 0 :(得分:4)
很有建议你在服务器端使用JScript而不是VBScript。它不仅更自然地做这种事情,你很可能熟悉这种语言。缺点是Web上与ASP相关的绝大多数“操作方法”都是用VBScript编写的。
VBScript中的关联数组称为Dictionary
可从Scripting
库获得。然而,要创建一个层次结构,您可能需要更多的帮助。我会围绕Dictionary
创建一个类,这样我就能拥有更多只有Name
属性的东西,并且可以更容易地进行层次操作。
以下是一个示例类: -
Class Node
Private myName
Private myChildren
Private Sub Class_Initialize()
Set myChildren = CreateObject("Scripting.Dictionary")
End Sub
Public Property Get Name()
Name = myName
End Property
Public Property Let Name(value)
myName = Value
End Property
Public Function AddChild(value)
If Not IsObject(value) Then
Set AddChild = new Node
AddChild.Name = value
Else
Set AddChild = value
End If
myChildren.Add AddChild.Name, AddChild
End Function
Public Default Property Get Child(name)
Set Child = ObjectOrNothing(myChildren.Item(name))
End Property
Public Property Get Children()
Set Children = myChildren
End Property
Private Function ObjectOrNothing(value)
If IsObject(value) Then
Set ObjectOrNothing = value
Else
Set ObjectOrNothing = Nothing
End If
End Function
End Class
现在您可以创建树: -
Dim root : Set root = new Node
With root.AddChild("United States")
With .AddChild("Washington")
With .AddChild("Electric City")
.AddChild "Banks Lake"
End With
With .AddChild("Lake Chelan")
.AddChild "Wapato Point"
End With
.AddChild "Gig Harbour"
End With
End With
现在访问此heirarchy: -
Sub WriteChildrenToResponse(node)
For Each key In node.Children
Response.Write "<div class=""node"">" & vbCrLf
Response.Write "<div>" & root.Child(key).Name "</div>" & vbCrlF
Response.Write "<div class=""children"">" & vbCrLf
WriteChildrenToResponse root.Child(key)
Response.Write "</div></div>"
Next
End Sub
''# Dump content of root heirarchy to the response
WriteChildrenToResponse root
答案 1 :(得分:1)
答案 2 :(得分:0)
使用Server.CreateObject(“Scripting.Dictionary”)
http://msdn.microsoft.com/en-us/library/x4k5wbx4(VS.85).aspx
答案 3 :(得分:0)
您可能想看一下sql端的物化路径模型。这真的很简单,使用相对'静态'树。
答案 4 :(得分:0)
我会创建一个包含这些的类,如下所示:
Class Area
Public Name
Private _children
Private Sub Class_Initialize()
_children = Array()
End Sub
Public Property Get Children()
Set Children = _children
End Property
Public Sub AddChild(newChild)
ReDim Preserve _children(UBound(_children)+1)
Set _children(UBound(_children)) = newChild
End Sub
End Class