【问题标题】:Display data in categorywise按类别显示数据
【发布时间】:2012-11-21 10:47:38
【问题描述】:

我有一个名为类别的列表。它包含以下数据。

Name  -                  Id 
----------------------------
Mobile  -                 0
Cellphones -             21
Mobile Accessories -     22
Camera -                  0
Camcorder -              55
Camera Accessories       60
SLRs                     61
Photo Printers           63
Computers                0
Laptops                  65
Memory Cards             67
Monitors                 69
RAM                      70
Computer Accessories     71

我想使用 Ul 和 Li 标签按以下方式显示上述数据:

移动

-手机

-手机配件

相机

-摄像机

-相机配件

-单反

-照片打印机

计算机

-笔记本电脑

-存储卡

-监视器

-内存

-电脑配件

即id=0表示父类,id=非零表示子类

帮助我。在此先感谢。

【问题讨论】:

  • 你有没有尝试过?
  • 您好,欢迎来到 StackOverFlow。但愿你更具体!.

标签: asp.net-mvc-3 c#-4.0


【解决方案1】:
Namespace Test

Public Class Class1

    Dim Categories As List(Of Category) = New List(Of Category) _
                                          From {New Category With {
                                                .ID = 0, .Name = "Mobile",
                                                .Items = New List(Of CategoryItem) _
                                                    From {New CategoryItem With {.ID = 1, .Name = "Cellphones"},
                                                          New CategoryItem With {.ID = 2, .Name = "Mobile Accessories"}
                                                         }
                                                },
                                                New Category With {
                                                .ID = 1, .Name = "Camera",
                                                .Items = New List(Of CategoryItem) _
                                                    From {New CategoryItem With {.ID = 1, .Name = "Camcorder"},
                                                          New CategoryItem With {.ID = 2, .Name = "Camera Accessories"},
                                                          New CategoryItem With {.ID = 3, .Name = "SLRs"},
                                                          New CategoryItem With {.ID = 4, .Name = "Photo Printers"}
                                                         }
                                                },
                                                New Category With {
                                                .ID = 2, .Name = "Computers",
                                                .Items = New List(Of CategoryItem) _
                                                    From {New CategoryItem With {.ID = 1, .Name = "Laptops"},
                                                          New CategoryItem With {.ID = 2, .Name = "Memory Cards"},
                                                          New CategoryItem With {.ID = 3, .Name = "Monitors"},
                                                          New CategoryItem With {.ID = 4, .Name = "RAM"},
                                                          New CategoryItem With {.ID = 5, .Name = "Computer Accessories"}
                                                         }
                                                }
                                                }

    Public Sub New()
     Dim ul As String =  SetCategories()
    End Sub

    Private Function SetCategories() As String
        Dim ulCategories As XElement = <ul class="float-left"></ul>

        For Each cat As Category In Categories
            Dim currentCategory As Category = cat
            Dim liCategory As XElement = <li><%= currentCategory.Name %></li>
            Dim ulCategoryItems As XElement = <ul></ul>
            For Each item As CategoryItem In currentCategory.Items
                Dim currentItem As CategoryItem = item
                Dim liItem As XElement = <li><%= currentItem.Name %></li>
                ulCategoryItems.Add(liItem)
            Next item
            liCategory.Add(ulCategoryItems)
            ulCategories.Add(liCategory)
        Next cat

        Return ulCategories.ToString()

    End Function

End Class

Public Class Category

    Public Property ID As Integer
    Public Property Name As String
    Public Property Items As List(Of CategoryItem)

End Class

Public Class CategoryItem
    Public Property ID As Integer
    Public Property Name As String
End Class

End Namespace

【讨论】:

  • 除了你的代码段,你还应该简要解释你的答案。
【解决方案2】:

在这里,把它放在你的视野中:

<ul>
    <li>Mobile
      <ul>
          <li>Cellphones</li>
          <li>Mobile Accessories</li>
      </ul>
    </li>    
    <li>Camera
      <ul>
          <li>Camcorder</li>
          <li>Camera Accessories</li>
          <li>SLRs</li>
          <li>Photo Printers</li>
      </ul>
    </li>    
    <li>Computers
      <ul>
          <li>Laptops</li>
          <li>Memory Cards</li>
          <li>Monitors</li>
          <li>RAM</li>
          <li>Computer Accessories</li>
      </ul>
    </li>    
</ul>

虽然这是您所要求的,但我认为您还想要更多。

由于您的问题没有任何具体内容或您的任何尝试,因此在以下解决方案中做出了几个假设。

类:

public class Category
{
  public int Id { get; set; }
  public string Name { get; set; }
  public List<Category> Subcateogries { get; set; }

  public Category()
  {
    Subcateogries = new List<Category>();
  }
}

控制器:

public ActionResult Index()
{
  //The Data
  var categories = new List<Category>
  {
    new Category { Id = 0, Name = "Mobile" },
    new Category { Id = 21, Name = "Cellphones" },
    new Category { Id = 22, Name = "Mobile Accessories" },
    new Category { Id = 0, Name = "Camera" },
    new Category { Id = 55, Name = "Camcorder" },
    new Category { Id = 60, Name = "Camera Accessories" },
    new Category { Id = 61, Name = "SLRs" },
    new Category { Id = 63, Name = "Photo Printers" },
    new Category { Id = 0, Name = "Computers" },
    new Category { Id = 65, Name = "Laptops" },
    new Category { Id = 67, Name = "Memory Cards" },
    new Category { Id = 69, Name = "Monitors" },
    new Category { Id = 70, Name = "RAM" },
    new Category { Id = 71, Name = "Computer Accessories" }
  };

  var GroupedData = new List<Category>();

  foreach(var category in categories)
  {
    if(category.Id == 0)
    {
      GroupedData.Add(category);
    }
    else
    {
      GroupedData.Last().Subcateogries.Add(category);
    }
  }

  return View(GroupedData);
}

查看:

@model List<Category>

<h2>Categories</h2>

<ul>
  @foreach (var topCategory in Model)
  {
    <li>@topCategory.Name
      <ul>
        @foreach (var subCategory in topCategory.Subcateogries)
        {
          <li>@subCategory.Name</li>
        }
      </ul>
    </li>    
  }
</ul>

这是假设起始列表(在本例中为 categories 是您想要的顺序)。在创建 GroupedData 时,我还通过重用原始列表项来走捷径。还假设永远只有两个级别。

可以重构视图以利用部分视图并处理多个级别,但这不是问题的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多