views:

26

answers:

1

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
A: 

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
Daniel Dyson