public async Task <ApiResult> TreeRoleIdAsync(int roleId) { var list = new List <MenuTreeOutput>(); var existMenuId = await Db.Queryable <R_Role_Menu>().Where(d => d.IsPass && d.RoleId == roleId && d.IsPass).Select(d => d.MenuId).ToListAsync(); var allMenus = await GetListAsync(d => d.Status); if (allMenus.Count <= 0 || allMenus == null) { return(new ApiResult(allMenus)); } foreach (var item in allMenus) { if (item.ParentId != 0) { continue; } var menuTreeOutput = new MenuTreeOutput() { Id = item.Id, Title = item.Name, Checked = existMenuId.FirstOrDefault(d => d == item.Id) != 0, Children = AddChildNode(allMenus, item.Id, existMenuId), }; list.Add(menuTreeOutput); } return(new ApiResult(list)); }
private List <MenuTreeOutput> AddChildNode(IEnumerable <Menu> data, int parentId, List <int> existMenuId) { var list = new List <MenuTreeOutput>(); var data2 = data.Where(d => d.ParentId == parentId).OrderBy(d => d.Name).ToList(); foreach (var item in data2) { var menuTreeOutput = new MenuTreeOutput() { Id = item.Id, Title = item.Name, Checked = existMenuId.FirstOrDefault(d => d == item.Id) != 0, Children = AddChildNode(data, item.Id, existMenuId) }; list.Add(menuTreeOutput); } return(list); }