用于在来自自连接表的嵌套 <ul>s 中显示无限类别树的逻辑 [英] Logic for displaying infinite category tree in nested <ul>s from Self Join Table

查看:25
本文介绍了用于在来自自连接表的嵌套 <ul>s 中显示无限类别树的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我解决我的大问题.
在我的在线购物项目中,我创建了一个动态类别列表(具有无限级深度),并在 DB 中的单个表中使用自联接实现.架构如下:

(来源:aspalliance.com)

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:

(source: aspalliance.com)

更新
我想使用 JQuery 插件来制作多级菜单栏.这个插件使用

  • 元素,所以我应该将数据库表转换为
    • .结果应该是这样的:

      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>
      

      我可以使用嵌套数据控件(如转发器控件),但您知道,使用此解决方案,我只能实现具有非无限层次树结构的列表.
      请帮帮我!任何建议?我在网上用谷歌搜索,但没有找到合适的方法.我使用 ASP.net 3.5 和 LINQ.
      最好的方法是什么?

      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?

      推荐答案

      使用这种递归方法

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

      这篇关于用于在来自自连接表的嵌套 &lt;ul&gt;s 中显示无限类别树的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆