用遞歸實現無限級菜單,產品分類,蓋樓式評論、留言等功能。
下列代碼不能直接使用
CREATE TABLE [dbo].[P_Category]( [Code] [varchar](36) NOT NULL PRIMARY KEY, [Parent_Code] [varchar](36) NULL, [Depth] [int] NULL, [Name] [varchar](50) NULL ) GO INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'101', N'1', 2, N'木門') INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'10101', N'101', 3, N'室內木門') INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'1010101', N'10101', 4, N'A02') INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'101010101', N'10101', 5, N'A01') INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'10101010101', N'101010101', 6, N'A0101') INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'102', N'1', 2, N'B') INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'10201', N'102', 3, N'B1') INSERT [dbo].[P_Category] ([Code], [Parent_Code], [Depth], [Name]) VALUES (N'1020101', N'10201', 4, N'B2')
private List<Category> CategoryCacheAllList { get; set; } [Route("")] public HttpResponseMessage Get() { var list = CacheHelper<List<Category>>.GetCache(CategoryAllListCacheKEY); if (list == null) { CategoryCacheAllList = CategoryService.GetCacheList(); //取得數據庫里面所有數據 list = new List<Category>(); CategoryJson(list, "1"); CacheHelper<List<Category>>.SetCache(CategoryAllListCacheKEY, list); } return Request.CreateResponse(HttpStatusCode.OK, list); //下面的代碼這個沒試 //string json = JsonConvert.SerializeObject(categoryList, Formatting.Indented); //return json; } /// <summary> /// 取得兄弟節點 /// </summary> /// <param name="categoryList"></param> /// <param name="parentCode"></param> public void CategoryJson(List<Category> categoryList, string parentCode) { var list = CategoryCacheAllList.FindAll(p => p.ParentCode == parentCode); if (list.Count > 0) { foreach (var item in list) { CategoryTreeJson(item, item.Code); categoryList.Add(item); } } } /// <summary> /// 遞歸出子對象 /// </summary> /// <param name="sbCategory"></param> /// <param name="parentCode"></param> private void CategoryTreeJson(Category sbCategory, string parentCode) { var list = CategoryCacheAllList.FindAll(p => p.ParentCode == parentCode); if (list.Count > 0) { sbCategory.Children = new List<Category>(); foreach (var item in list) { CategoryTreeJson(item, item.Code); sbCategory.Children.Add(item); } } }
namespace VipSoft.Base.Core.Entity { /// <summary> /// 產品分類 /// </summary> [Table("VipSoft_Category")] public class Category { /// <summary> /// 編碼 /// </summary> [Column(ColumnType.IncrementPrimary, Name = "Code")] public string Code { get; set; } /// <summary> /// 父級編碼 /// </summary> [Column(Name = "Parent_Code")] public string ParentCode { get; set; } /// <summary> /// 深度 /// </summary> [Column(Name = "Depth")] public int? Depth { get; set; } /// <summary> /// 分類名稱 /// </summary> [Column(Name = "Name")] public string Name { get; set; } /// <summary> /// 排序 /// </summary> [Column(Name = "Sequence")] public int? Sequence { get; set; } /// <summary> /// 狀態 /// </summary> [Column(Name = "Status")] public int? Status { get; set; } /// <summary> /// 創建時間 /// </summary> [Column(Name = "Create_Date")] public DateTime? CreateDate { get; set; } public List<Category> Children { get; set; } } }