VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C# 构造tree菜单工具方法

  • 2019-03-14 16:05 来源:未知

如何构造tree数据结构,做个笔记,方便查阅,本方法是直接返回json字符串:

复制代码
        private string ToMenuJson(List<Model> data, string parentId)
        {
            var sbJson = new StringBuilder();
            sbJson.Append("[");
            var entitys = data.FindAll(t => t.ParentID == parentId).ToList();
            if (entitys.Count > 0)
            {
                foreach (var item in entitys)
                {
                    var strJson = JsonConvert.SerializeObject(item, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
                    strJson = strJson.Insert(strJson.Length - 1, ",\"ChildNodes\":" + ToMenuJson(data, item.ID) + "");
                    sbJson.Append(strJson + ",");
                }
                sbJson = sbJson.Remove(sbJson.Length - 1, 1);
            }
            sbJson.Append("]");
            return sbJson.ToString();
        }
复制代码

 本方法是构造tree实体,返回实体:

复制代码
        /// <summary>
        /// 绑定列表递归方法
        /// </summary>
        /// <param name="ddl">构造tree数据结构返回的实体</param>
        /// <param name="menus">绑定的数据源</param>
        /// <param name="categoryid">父级id值</param>
        private void BindChannelType(List<ECategoryModel> ddl, List<ECategory> menus, Guid categoryid)
        {
            List<ECategory> filterMenu = menus.Where(w => w.ParentID == categoryid).ToList();

            foreach (var m in filterMenu)
            {
                var mextend = new ECategoryModel
                {
                    ID = m.ID,
                    ParentID = m.ParentID,
                    CategoryName = m.CategoryName,
                    URL = m.URL,
                    Sort = m.Sort,
                    IsDelete = m.IsDelete
                };
                ddl.Add(mextend);
                BindChannelType(mextend.children, menus, m.ID);
            }
        }
复制代码

 

构造树形数据(带空格和缩进):

 

复制代码
        /// <summary>
        /// 构造树形下拉框数据结构
        /// </summary>
        private void Generate(List<ECategoryModel> viewList, List<ECategory> categories, Guid categoryid, int i = 0)
        {
            i++;
            var filterMenu = categories.Where(w => w.ParentID == categoryid).OrderBy(w => w.Sort).ToList();

            foreach (var m in filterMenu)
            {
                var str = "";
                //父级菜单不缩进  
                for (var j = 1; j < i; j++)
                {
                    str += HttpUtility.HtmlDecode("&nbsp;&nbsp;") + "├";
                }

                var mextend = new ECategoryModel
                {
                    ID = m.ID,
                    ParentID = m.ParentID,
                    CategoryName = $"{str}{m.CategoryName}",
                    URL = m.URL,
                    iconSkin = m.URL,
                    Sort = m.Sort,
                    IsDelete = m.IsDelete
                };
                viewList.Add(mextend);
                Generate(viewList, categories, m.ID, i);
            }
        }
复制代码

 

 

相关的实体类 ECategoryModel.cs

复制代码
using System;
using System.Collections.Generic;

namespace MSEduPlatform.Models
{
    /// <summary>
    /// ECategory表实体类
    /// </summary>
    public class ECategoryModel
    {
        /// <summary>
        /// 菜单ID
        /// </summary>          
        public Guid ID { get; set; }

        /// <summary>
        /// 父级菜单ID
        /// </summary>          
        public Guid ParentID { get; set; }

        /// <summary>
        /// 菜单Code
        /// </summary>          
        public string Code { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>          
        public string CategoryName { get; set; }

        /// <summary>
        /// 菜单地址
        /// </summary>          
        public string URL { get; set; }

        /// <summary>
        /// 菜单排序
        /// </summary>          
        public int Sort { get; set; }

        /// <summary>
        /// 显示状态
        /// </summary>          
        public int States { get; set; }

        /// <summary>
        /// 栏目权限类别
        /// </summary>          
        public string AuthorizationType { get; set; }

        /// <summary>
        /// 是否授权
        /// </summary>          
        public bool IsAuthorization { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>          
        public Guid Creater { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>          
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>          
        public Guid Modify { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>          
        public DateTime ModifyTime { get; set; }

        /// <summary>
        /// 是否逻辑删除
        /// </summary>          
        public int IsDelete { get; set; }

        /// <summary>
        /// 子数据列表
        /// </summary>
        public List<ECategoryModel> children = new List<ECategoryModel>();
    }
}

相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号