逻辑嵌套&LT显示无限的类别树; UL> S从自加入表 [英] Logic for displaying infinite category tree in nested <ul>s from Self Join Table

查看:154
本文介绍了逻辑嵌套&LT显示无限的类别树; UL> S从自加入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请帮我解决我的大问题。

我在网上购物的项目,我创建了一个动态分类列表(具有无穷级深度)在一个表中实现与数据库自加盟。
该架构是象下面这样:


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:

更新结果
我想用一个jQuery插件,使多级菜单栏。这个插件使用< UL> <李> 元素,所以我应该转换数据表为< UL> <立GT; 。结果应该是这样的:

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>

我可以使用嵌套的数据控件(如Repeater控件),但你要知道,这个解决方案,我只是可以实现与非无限分层树形结构的列表。

请帮帮我!任何建议?我用Google搜索网络,但没有找到合适的方法。我使用的 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天全站免登陆