C#之生成树形菜单数据

public class MenuModel
    {
        /// <summary>
        /// ID
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 菜单名
        /// </summary>
        public string MenuName { get; set; }
        /// <summary>
        /// 父菜单
        /// </summary>
        public int ParentID { get; set; }
    }
 public class TreeModel
    {
        /// <summary>
        /// ID
        /// </summary>
        public string id { get; set; }

        /// <summary>
        /// 节点名称
        /// </summary>
        public string text { get; set; }
        
        /// <summary>
        /// 是否展开
        /// </summary>
        public string state  { get; set; }

        /// <summary>
        /// 图标样式
        /// </summary>
        public string iconCls { get; set; }


        /// <summary>
        /// 子节点集合
        /// </summary>
        public List<TreeModel> children { get; set; }
        
    }
 public List<TreeModel> GetTreeList(int ParentID)
        {
            List<TreeModel> TreeList = new List<TreeModel>();
            List<MenuModel> ModelList = GetChildMenuList(ParentID);
            foreach (var item in ModelList)
            {
                TreeModel m = new TreeModel();
                m.id = item.ID.ToString();
                m.text = item.MenuName.ToString();
                m.children = GetTreeList(item.ID);
                TreeList.Add(m);
            }
            return TreeList;
        }
        /// <summary>
        /// 获取所有菜单数据
        /// </summary>
        /// <returns></returns>
        public List<MenuModel> GetAllMenuList()
        {
            List<MenuModel> list = new List<MenuModel>();
            list.Add(new MenuModel { ID = 1, MenuName = "菜单1", ParentID = 0 });
            list.Add(new MenuModel { ID = 2, MenuName = "菜单1.1", ParentID = 1 });
            list.Add(new MenuModel { ID = 3, MenuName = "菜单1.1.1", ParentID = 2 });
            list.Add(new MenuModel { ID = 4, MenuName = "菜单2", ParentID = 0 });
            return list;
        }
        /// <summary>
        /// 根据父节点获取子节点
        /// </summary>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        public List<MenuModel> GetChildMenuList(int ParentID)
        {
            List<MenuModel> list = GetAllMenuList();
            var result =list.Where(x => x.ParentID == ParentID);
            return result.ToList();
        }

 

 

调用:

List<TreeModel> list = GetTreeList(0);
string json = new JavaScriptSerializer().Serialize(list);

 

结果为

[

    {

        "id": "1",

        "text": "菜单1",

        "state": null,

        "iconCls": null,

        "children": [

            {

                "id": "2",

                "text": "菜单1.1",

                "state": null,

                "iconCls": null,

                "children": [

                    {

                        "id": "3",

                        "text": "菜单1.1.1",

                        "state": null,

                        "iconCls": null,

                        "children": []

                    }

                ]

            }

        ]

    },

    {

        "id": "4",

        "text": "菜单2",

        "state": null,

        "iconCls": null,

        "children": []

    }

]

 

上一篇:FastReport教程:MS SQL中的递归


下一篇:docker 实战---部署tomcat\weblogic集群 (二)