从此数据类输出数据
请有人帮助使用哪种控件/方法将此类中的数据获取到嵌套列表中,即父项、子项、孙子、曾孙?
当我运行时:
Dim menu As New MenuBuilder
Response.Write(menu.BuildMenu().ToString)
我只得到顶级链接,没有子级别的链接,
谢谢。
Public Class MenuBuilder
Private _rawData As List(Of MenuData) = MenuDataImport.GetMenuData()
Public Function BuildMenu() As String
'create a new list with the top level0 items only
Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))
'sort the top level items
builtMenu.Sort(Function(a, b) a.OrderID - b.OrderID)
'get the children items
For Each menuData As MenuData In builtMenu
If menuData.Children IsNot Nothing Then
menuData.Children.AddRange(GetChildren(menuData))
End If
Next
Dim Builder = New StringBuilder()
'builtMenu contains the right items with the right children in order.
For Each menuData As MenuData In builtMenu
If menuData.Url IsNot Nothing And menuData.Moduleid = 0 Then
'format as a standard link
Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", menuData.Url, menuData.Urltarget, menuData.Heading)
ElseIf InStr(menuData.Url, "file:///") Then
'format as a file link
Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", menuData.Url, menuData.Urltarget, menuData.Heading)
Else
'format as a querystring id link
Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", menuData.Id, menuData.Heading)
End If
If menuData.Children IsNot Nothing Then
For Each child As MenuData In menuData.Children
If child.Url IsNot Nothing And child.Moduleid = 0 Then
'format as a standard link
Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", child.Url, child.Urltarget, child.Heading)
ElseIf InStr(child.Url, "file:///") Then
'format as a file link
Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", child.Url, child.Urltarget, child.Heading)
Else
'format as a querystring id link
Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", child.Id, child.Heading)
End If
For Each grandChild As MenuData In child.Children
If grandChild.Url IsNot Nothing And grandChild.Moduleid = 0 Then
'format as a standard link
Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", grandChild.Url, grandChild.Urltarget, grandChild.Heading)
ElseIf InStr(grandChild.Url, "file:///") Then
'format as a file link
Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", grandChild.Url, grandChild.Urltarget, grandChild.Heading)
Else
'format as a querystring id link
Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", grandChild.Id, grandChild.Heading)
End If
For Each greatgrandChild As MenuData In grandChild.Children
If greatgrandChild.Url IsNot Nothing And greatgrandChild.Moduleid = 0 Then
'format as a standard link
Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", greatgrandChild.Url, greatgrandChild.Urltarget, greatgrandChild.Heading)
ElseIf InStr(grandChild.Url, "file:///") Then
'format as a file link
Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", greatgrandChild.Url, greatgrandChild.Urltarget, greatgrandChild.Heading)
Else
'format as a querystring id link
Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", greatgrandChild.Id, greatgrandChild.Heading)
End If
Next
Next
Next
End If
Next
Return Builder.ToString()
End Function
Private Function GetChildren(ByVal parent As MenuData) As List(Of MenuData)
Dim children As List(Of MenuData) = _rawData.FindAll(Function(item) item.GroupingID = parent.Id)
'sort the menu items
children.Sort(Function(a, b) a.OrderID - b.OrderID)
'get children's children
For Each menuData As MenuData In children
If menuData.Children IsNot Nothing Then
menuData.Children.AddRange(GetChildren(menuData))
End If
Next
Return children
End Function
End Class
Please can someone help with which controls / method to use to get data out of this class into nested lists, i.e parent, child, grandchild, greatgrandchild?
When I run:
Dim menu As New MenuBuilder
Response.Write(menu.BuildMenu().ToString)
I only get the top level links and none of the child levels
Thanks.
Public Class MenuBuilder
Private _rawData As List(Of MenuData) = MenuDataImport.GetMenuData()
Public Function BuildMenu() As String
'create a new list with the top level0 items only
Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))
'sort the top level items
builtMenu.Sort(Function(a, b) a.OrderID - b.OrderID)
'get the children items
For Each menuData As MenuData In builtMenu
If menuData.Children IsNot Nothing Then
menuData.Children.AddRange(GetChildren(menuData))
End If
Next
Dim Builder = New StringBuilder()
'builtMenu contains the right items with the right children in order.
For Each menuData As MenuData In builtMenu
If menuData.Url IsNot Nothing And menuData.Moduleid = 0 Then
'format as a standard link
Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", menuData.Url, menuData.Urltarget, menuData.Heading)
ElseIf InStr(menuData.Url, "file:///") Then
'format as a file link
Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", menuData.Url, menuData.Urltarget, menuData.Heading)
Else
'format as a querystring id link
Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", menuData.Id, menuData.Heading)
End If
If menuData.Children IsNot Nothing Then
For Each child As MenuData In menuData.Children
If child.Url IsNot Nothing And child.Moduleid = 0 Then
'format as a standard link
Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", child.Url, child.Urltarget, child.Heading)
ElseIf InStr(child.Url, "file:///") Then
'format as a file link
Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", child.Url, child.Urltarget, child.Heading)
Else
'format as a querystring id link
Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", child.Id, child.Heading)
End If
For Each grandChild As MenuData In child.Children
If grandChild.Url IsNot Nothing And grandChild.Moduleid = 0 Then
'format as a standard link
Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", grandChild.Url, grandChild.Urltarget, grandChild.Heading)
ElseIf InStr(grandChild.Url, "file:///") Then
'format as a file link
Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", grandChild.Url, grandChild.Urltarget, grandChild.Heading)
Else
'format as a querystring id link
Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", grandChild.Id, grandChild.Heading)
End If
For Each greatgrandChild As MenuData In grandChild.Children
If greatgrandChild.Url IsNot Nothing And greatgrandChild.Moduleid = 0 Then
'format as a standard link
Builder.AppendFormat("<a href=""http://{0}"" target=""{1}"">{2}</a>", greatgrandChild.Url, greatgrandChild.Urltarget, greatgrandChild.Heading)
ElseIf InStr(grandChild.Url, "file:///") Then
'format as a file link
Builder.AppendFormat("<a href={0} target=""{1}"">{2}</a>", greatgrandChild.Url, greatgrandChild.Urltarget, greatgrandChild.Heading)
Else
'format as a querystring id link
Builder.AppendFormat("<a href=Default.aspx?id={0}>{1}</a>", greatgrandChild.Id, greatgrandChild.Heading)
End If
Next
Next
Next
End If
Next
Return Builder.ToString()
End Function
Private Function GetChildren(ByVal parent As MenuData) As List(Of MenuData)
Dim children As List(Of MenuData) = _rawData.FindAll(Function(item) item.GroupingID = parent.Id)
'sort the menu items
children.Sort(Function(a, b) a.OrderID - b.OrderID)
'get children's children
For Each menuData As MenuData In children
If menuData.Children IsNot Nothing Then
menuData.Children.AddRange(GetChildren(menuData))
End If
Next
Return children
End Function
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
公共类菜单生成器
Private _rawData As List(Of MenuData) = MenuDataImport.GetMenuData()
Public Class MenuBuilder
Private _rawData As List(Of MenuData) = MenuDataImport.GetMenuData()