列出类似淘宝网站的分类菜单


在ui 界面中列出所有的分类以及子分类目录菜单,假设菜单只使用最多为三级的菜单,具体的做法如下:

数据库的设计

 

一级分类的设计:

   public class CategoryA : CategoryC
    {
     /// <summary>
     ///  二级分类集合
     /// </summary>
        public IEnumerable<CategoryB> ListCategoryB { get; set; }

    }

二级分类的设计:

  public  class CategoryB : CategoryC
    {

        /// <summary>
        ///  三级分类集合
        /// </summary>
        public IEnumerable<CategoryC> ListCategoryC { get; set; }
    }

三级分类的设计:

// <summary>
   /// 三级分类 ( 末级)
   /// </summary>
   public class CategoryC
    {

        /// <summary>
        /// 类别ID
        /// </summary>
        public short ItemClsid { get; set; }

        /// <summary>
        /// 标识
        /// </summary>
        public string ItemFlag { get; set; }
        /// <summary>
        /// 类别编码
        /// </summary>
        public string ItemClsno { get; set; }

        /// <summary>
        /// 类别名称
        /// </summary>
        public string ItemClsname { get; set; }
        /// <summary>
        /// 父级编码
        /// </summary>
        public string UpClsno { get; set; }

    }

菜单类的设计:

    public class Category 
    {

        public  IEnumerable<CategoryA> ListCategoryA { get; set; }

    }

获取菜单的设计:

/// <summary>
        /// 获取全部的分类
        /// </summary>
        /// <returns></returns>
        public Category GetAllCategory()
        {
            try
            {
                //分类菜单
                Category myCategory = new Category();

               // 一级分类
               myCategory.ListCategoryA = ClRep.GetAllCategoryA().ToList();

               if (myCategory.ListCategoryA != null && myCategory.ListCategoryA.Count() > 0)
                {
                    // 遍历一级分类
                    foreach (var item in myCategory.ListCategoryA)
                    {
                        // 判断一级分类编码是否为空
                        if (!string.IsNullOrEmpty(item.ItemClsno))
                        {
                            // 获取一级分类下的对应的二级分类
                           item.ListCategoryB = ClRep.GetAllCategoryB(item.ItemClsno);

                            if (item.ListCategoryB != null)
                            {
                                // 在二级分类不是空时, 向二级分类集合中添加集合
                                item.ListCategoryB.ToList().AddRange(item.ListCategoryB);
                            }

                            // 判断二级分类是否为空
                            if (item.ListCategoryB != null && item.ListCategoryB.Count() > 0)
                            {
                                // 遍历二级分类
                                foreach (var t in item.ListCategoryB)
                                {
                                    // 判断二级分类编码是否为空
                                    if (!string.IsNullOrEmpty(t.ItemClsno))
                                    {
                                        //获取二级分类下对应的三级分类
                                        t.ListCategoryC = ClRep.GetAllCategoryC(t.ItemClsno);

                                        if (t.ListCategoryC != null)
                                        {
                                            // 在三级分类不是空时,向三级分类集合中添加集合
                                            t.ListCategoryC.ToList().AddRange(t.ListCategoryC);
                                        }
                                       
                                    }
                                }
                            }
                        }
                    }
                }

                return myCategory;
            }

            catch (Exception)
            {
                return null;
            }

        }

运行效果:

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值