/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { var sections = new List<Section> { new Section { Id = 1, Name = "中國", ParentID = 0 }, new Section { Id = 2, Name = "江西", ParentID = 1 }, new Section { Id = 3, Name = "江蘇", ParentID = 1 }, new Section { Id = 4, Name = "南京", ParentID = 3 }, new Section { Id = 5, Name = "南昌", ParentID = 2 }, new Section { Id = 6, Name = "東湖區", ParentID = 5 }, new Section { Id = 7, Name = "廣東", ParentID = 1 }, new Section { Id = 8, Name = "深圳", ParentID = 7 }, new Section { Id = 9, Name = "羅湖區塗聚文", ParentID = 8 } }; //sections = sections.OrderBy(x => x.ParentID).ThenBy(x => x.Name).ToList(); //var stack = new Stack<Section>(); //// Grab all the items without parents //foreach (var section in sections.Where(x => x.ParentID == default(int)).Reverse()) //{ // stack.Push(section); // sections.RemoveAt(0); //} var output = new List<Section>(); //while (stack.Any()) //{ // var currentSection = stack.Pop(); // var children = sections.Where(x => x.ParentID == currentSection.Id).Reverse(); // foreach (var section in children) // { // stack.Push(section); // sections.Remove(section); // } // output.Add(currentSection); //} //sections = output; List<MySection> mys = MenuHelper.GetMyMenuCollection(sections); //ResolveDDL<MySectionMenu>(mys); var outputlist = (from mysection in mys orderby mysection.TreeLevel descending select mysection).ToList(); var outputstringlist = (from mysection in mys orderby mysection.TreeLevel descending select mysection.Name).ToList(); for (int i = 0; i < outputlist.Count; i++) { //Response.Write(string.Format("ID:{0} ParentID: {1} TreeLevel: {2} Name:{3}<br/>", mys[i].Id, mys[i].ParentID, mys[i].TreeLevel,mys[i].Name)); Response.Write(string.Format("ID:{0} ParentID: {1} TreeLevel: {2} Name:{3}<br/>", outputlist[i].Id, outputlist[i].ParentID, outputlist[i].TreeLevel, outputlist[i].Name)); } } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mys"></param> protected void ResolveDDL<T>(List<T> mys) where T : MyBaseSection, new() { ResolveDDL<T>(mys, -1, true); } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mys"></param> /// <param name="currentId"></param> protected void ResolveDDL<T>(List<T> mys, int currentId) where T : MyBaseSection, new() { ResolveDDL<T>(mys, currentId, true); } /// <summary> /// 將一個樹型結構放在一個下列列表中可供選擇 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="currentId"></param> /// <param name="mys"></param> protected void ResolveDDL<T>(List<T> mys, int currentId, bool addRootNode) where T : MyBaseSection, new() { if (addRootNode) { // 所有節點的TreeLevel加一,然後添加根節點 foreach (T my in mys) { my.TreeLevel += 1; } T root = new T(); root.Name = "--根節點--"; root.Id = 0; root.TreeLevel = 0; mys.Insert(0, root); } // currentId==-1表示當前節點不存在 if (currentId != -1) { // 本節點不可點擊(也就是說當前節點不可能是當前節點的父節點) // 並且本節點的所有子節點也不可點擊,你想如果當前節點跑到子節點的子節點,那麼這些子節點就從樹上消失了 bool startChileNode = false; int startTreeLevel = 0; foreach (T my in mys) { if (my.Id == currentId) { startTreeLevel = my.TreeLevel; my.Enabled = false; startChileNode = true; } else { if (startChileNode) { if (my.TreeLevel > startTreeLevel) { my.Enabled = false; } else { startChileNode = false; } } } } } } } /// <summary> /// / /// </summary> public class Section { public int Id { get; set; } public string Name { get; set; } public int ParentID { get; set; } } /// <summary> /// /// </summary> public class MySection { public int Id { get; set; } public string Name { get; set; } public int ParentID { get; set; } public int TreeLevel { get; set; } } /// <summary> /// /// </summary> public class MySectionMenu : MyBaseSection { } /// <summary> /// /// </summary> public class MyBaseSection { public int Id { get; set; } public int ParentId { get; set; } public string Name { get; set; } /// <summary> /// 本菜單在樹形結構中層級(從0開始) /// </summary> public int TreeLevel { get; set; } /// <summary> /// 是否可用(默認true) /// </summary> public bool Enabled { get; set; } /// <summary> /// 是否葉子節點(默認false) /// </summary> public bool IsTreeLeaf { get; set; } } /// <summary> /// /// </summary> public class MenuHelper { /// <summary> /// /// </summary> /// <param name="oldMenus"></param> /// <returns></returns> public static List<MySection> GetMyMenuCollection(List<Section> oldMenus) { List<MySection> newMenus = new List<MySection>(); ResolveMenuCollection(oldMenus, newMenus, 0, 0); return newMenus; } /// <summary> /// /// </summary> /// <param name="oldMenus"></param> /// <param name="newMenus"></param> /// <param name="parentId"></param> /// <param name="level"></param> /// <returns></returns> private static int ResolveMenuCollection(List<Section> oldMenus, List<MySection> newMenus, int parentId, int level) { int count = 0; foreach (Section menu in oldMenus) { if (menu.ParentID == parentId) { count++; MySection my = new MySection(); newMenus.Add(my); my.TreeLevel = level; my.Id = menu.Id; my.Name = menu.Name; my.ParentID = menu.ParentID; level++; int childCount = ResolveMenuCollection(oldMenus, newMenus, menu.Id, level); level--; } } return count; } }
顯示結果:
ID:1 ParentID: 0 TreeLevel: 0 Name:中國
ID:2 ParentID: 1 TreeLevel: 1 Name:江西
ID:5 ParentID: 2 TreeLevel: 2 Name:南昌
ID:6 ParentID: 5 TreeLevel: 3 Name:東湖區
ID:3 ParentID: 1 TreeLevel: 1 Name:江蘇
ID:4 ParentID: 3 TreeLevel: 2 Name:南京
ID:7 ParentID: 1 TreeLevel: 1 Name:廣東
ID:8 ParentID: 7 TreeLevel: 2 Name:深圳
ID:9 ParentID: 8 TreeLevel: 3 Name:塗聚文