Logic for displaying infinite category tree in nested
    s from Self Join Table

前端 未结 2 1536
旧时难觅i
旧时难觅i 2021-01-06 01:46

Please help me solve my big problem.
in my on-line shopping project i created a dynamic Category List (with Infinite Level Depth) Implemented in a Sing

相关标签:
2条回答
  • 2021-01-06 02:21

    Use this recursive method

    private string GenerateUL(IQueryable<Menu> menus)
    {
        var sb = new StringBuilder();
    
        sb.AppendLine("<ul>");
        foreach (var menu in menus)
        {
            if (menu.Menus.Any())
            {
                sb.AppendLine("<li>" + menu.Text);
                sb.Append(GenerateUL(menu.Menus.AsQueryable()));
                sb.AppendLine("</li>");
            }
            else
                sb.AppendLine("<li>" + menu.Text + "</li>");
        }
        sb.AppendLine("</ul>");
    
        return sb.ToString();
    }
    

    like this

    DataClasses1DataContext context = new DataClasses1DataContext();
    var s = GenerateUL(context.Menus.Where(m => m.ParentID == null));
    Response.Write(s);
    
    0 讨论(0)
  • 2021-01-06 02:30

    I think ASP.NET TreeView is your friend.

    Take a look here too.

    Also, you might want to use a dynamically created nested DataGrid or Repeater, here is an example (you can make it dynamic, so the nested (or even the parent) repeaters are genenerated programmatically.

    void GenerateChildren(Menu menu)
    {
        //Create DataGridRow/RepeaterRow/TreeViewNode/whatever for this menu
        menu.Children.Load();
        foreach (var child in menu.Children)
        {
            GenerateChildren(child);        
        }
    }
    

    Update See example 5 on this page. I would recommend generating the jQuery code by server, so it's easier for you to control both the menus and the jQ generated menus.

    0 讨论(0)
提交回复
热议问题