从此数据类输出数据

发布于 2024-09-06 21:16:22 字数 5268 浏览 8 评论 0原文

请有人帮助使用哪种控件/方法将此类中的数据获取到嵌套列表中,即父项、子项、孙子、曾孙?

当我运行时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过潦 2024-09-13 21:16:22

公共类菜单生成器
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()

    Builder.Append("<ul>")
    'builtMenu contains the right items with the right children in order.
    For Each menuData As MenuData In builtMenu
        Builder.Append("<li>")
        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
            Builder.Append("<ul>")
            For Each child As MenuData In menuData.Children
                Builder.Append("<li>")
                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

                If child.Children IsNot Nothing Then
                    Builder.Append("<ul>")
                    For Each grandChild As MenuData In child.Children
                        Builder.Append("<li>")
                        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

                        If grandChild.Children IsNot Nothing Then
                            Builder.Append("<ul>")
                            For Each greatgrandChild As MenuData In grandChild.Children
                                Builder.Append("<li>")
                                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
                                Builder.Append("</li>")
                            Next
                            Builder.Append("</ul>")
                        End If
                        Builder.Append("</li>")
                    Next
                    Builder.Append("</ul>")
                End If
                Builder.Append("</li>")
            Next
            Builder.Append("</ul>")
        End If
        Builder.Append("</li>")
    Next
    Builder.Append("</ul>")
    Return Builder.ToString()
End Function

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()

    Builder.Append("<ul>")
    'builtMenu contains the right items with the right children in order.
    For Each menuData As MenuData In builtMenu
        Builder.Append("<li>")
        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
            Builder.Append("<ul>")
            For Each child As MenuData In menuData.Children
                Builder.Append("<li>")
                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

                If child.Children IsNot Nothing Then
                    Builder.Append("<ul>")
                    For Each grandChild As MenuData In child.Children
                        Builder.Append("<li>")
                        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

                        If grandChild.Children IsNot Nothing Then
                            Builder.Append("<ul>")
                            For Each greatgrandChild As MenuData In grandChild.Children
                                Builder.Append("<li>")
                                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
                                Builder.Append("</li>")
                            Next
                            Builder.Append("</ul>")
                        End If
                        Builder.Append("</li>")
                    Next
                    Builder.Append("</ul>")
                End If
                Builder.Append("</li>")
            Next
            Builder.Append("</ul>")
        End If
        Builder.Append("</li>")
    Next
    Builder.Append("</ul>")
    Return Builder.ToString()
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文