用于在自连接表中的嵌套
    中显示无限类别树的逻辑

发布于 2024-09-15 01:54:02 字数 1609 浏览 3 评论 0原文

请帮我解决我的大问题。
在我的在线购物项目中,我创建了一个动态类别列表(具有无限级别深度),通过自连接在数据库中的单个表中实现。 架构如下所示:
alt 文本
(来源:aspalliance.com

更新
我想使用 JQuery 插件制作多级菜单栏。该插件使用

  • 元素,因此我应该将数据库表转换为
    • 。结果应该是这样的:

<ul>
  <li>Clothing 1
    <ul>
      <li>Trousers 2
        <ul>
          <li>Mens trousers 3</li>
          <li>Ladies trousers 3</li>
        </ul>
      </li> 
      <li>Jackets 2</li>
      <li>Shirts 2</li>
      <li>Shoes
        <ul>
          <li>Mens shoes 3
            <ul>
              <li>Mens formal shoes 4</li>
              <li>Mens casual shoes 4</li>
            </ul>
          </li>
          <li>Kids shoes 3</li>
          <li>Ladies shoes 3</li>
        </ul>
      </li>
    </ul>
   </li>
  <li>Cars 1
   <ul>
     <li>Small cars 2</i>
   </ul>
  </li>
</ul>

我可以使用嵌套数据控件(如转发器控件),但你知道,通过这个解决方案,我只能实现一个具有非无限分层树结构的列表。
请帮助我!有什么建议吗?我用谷歌搜索了网络,但没有找到合适的方法。我使用 ASP.net 3.5 和 LINQ。
最好的方法是什么?

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 Single Table in DB with Self join.
the schema is like below:
alt text
(source: aspalliance.com)

Update
I want to use a JQuery plugin to make a Multi Level Menu bar. this plugin uses <ul> and <li> elements so I should transform the DB table to <ul> and <li>. the result should like this:

<ul>
  <li>Clothing 1
    <ul>
      <li>Trousers 2
        <ul>
          <li>Mens trousers 3</li>
          <li>Ladies trousers 3</li>
        </ul>
      </li> 
      <li>Jackets 2</li>
      <li>Shirts 2</li>
      <li>Shoes
        <ul>
          <li>Mens shoes 3
            <ul>
              <li>Mens formal shoes 4</li>
              <li>Mens casual shoes 4</li>
            </ul>
          </li>
          <li>Kids shoes 3</li>
          <li>Ladies shoes 3</li>
        </ul>
      </li>
    </ul>
   </li>
  <li>Cars 1
   <ul>
     <li>Small cars 2</i>
   </ul>
  </li>
</ul>

I can use a nested data control(like repeater control) but you know, with this solution i just can implement a list with non-infinite hierarchical tree structure.
please help me! any suggestion?? I googled the web but not a suitable way found. I use ASP.net 3.5 and LINQ.

what is the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

输什么也不输骨气 2024-09-22 01:54:02

使用这种递归方法

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

像这样

DataClasses1DataContext context = new DataClasses1DataContext();
var s = GenerateUL(context.Menus.Where(m => m.ParentID == null));
Response.Write(s);

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);
原来是傀儡 2024-09-22 01:54:02

我认为 ASP.NET TreeView< /code>是你的朋友。

也请查看此处

另外,您可能想要使用动态创建的嵌套 DataGrid 或中继器,这里是一个示例(您可以将其设为动态,以便以编程方式生成嵌套(甚至父级)中继器。

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

更新
请参阅页面上的示例 5。
我建议通过服务器生成 jQuery 代码,这样您可以更轻松地控制菜单和 jQ 生成的菜单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文