如下代码如何将一个List的数据结构转换成一个 List<JsonTree>
public class JsonTreeMenu { public string label { get; set; } public string name { get; set; } public string id { get; set; } public string i { get; set; } public string link { get; set; } public List<JsonTreeMenu> children { get; set; } } public class SysMenu { public System.Guid Id { get; set; } // ID (Primary key) public string MenuName { get; set; } // MenuName (length: 50) public string MenuLink { get; set; } // MenuLink (length: 255) public string MenuSys { get; set; } // MenuSys (length: 50) public System.DateTime? CreateDateTime { get; set; } // CreateDateTime public int? CreateUserId { get; set; } // CreateUserID public System.DateTime? UpdateDateTime { get; set; } // UpdateDateTime public int? UpdateUserId { get; set; } // UpdateUserID public System.Guid? ParentId { get; set; } // ParentID public SysMenu() { CreateDateTime = System.DateTime.Now; } } public static List<SysMenu> GetAllSysMenu() { List<SysMenu> menus = new List<SysMenu>(); var im = new SysMenu() { Id = Guid.NewGuid(), MenuName = "信息部管理" }; menus.Add(im); menus.Add(new SysMenu() { Id = Guid.NewGuid(), ParentId = im.Id, MenuName = "账号数据配置", MenuLink = "SysProcessConfig" }); var bm = new SysMenu() { Id = Guid.NewGuid(), MenuName = "商务部管理" }; menus.Add(new SysMenu() { Id = Guid.NewGuid(), ParentId = bm.Id, MenuName = "账号审批", MenuLink = "Account/RegisterAudit" }); menus.Add(bm); var sm = new SysMenu() { Id = Guid.NewGuid(), MenuName = "库存数据查询" }; menus.Add(new SysMenu() { Id = Guid.NewGuid(), ParentId = sm.Id, MenuName = "产品进货", MenuLink = "ProductStoreIn" }); menus.Add(new SysMenu() { Id = Guid.NewGuid(), ParentId = sm.Id, MenuName = "销售流向数据", MenuLink = "SaleFlow" }); menus.Add(new SysMenu() { Id = Guid.NewGuid(), ParentId = sm.Id, MenuName = "库存数据", MenuLink = "StoreDetail" }); menus.Add(sm); return menus; //menus.Add(new SysMenu() { Id = Guid.NewGuid (), }); //using (MyDbContext ctx = new MyDbContext()) //{ // return ctx.SysMenus.ToList(); //} }
你这种结构,只有两层,连递归都不用,你自己循环两遍也能出来。。。
https://jsfiddle.net/kqw1qsf0/1/
最后这么解决的 很好用的js
https://jsfiddle.net/kqw1qsf0/1/
简单的递归