樹(shu)型結構(gou)~無限級聯(lian)下拉列(lie)表(biao)框
這(zhe)個問題困擾了(le)我很久,今天終于把它解決(jue)了(le),心(xin)中(zhong)的喜悅(yue)可(ke)想而知,趕(gan)快把它記錄一下
標(biao)題無限級聯下(xia)拉列(lie)表框(kuang)的含義:
可能有一(yi)個(ge)樹型結(jie)構的表,它(ta)可能有ID,Name,ParentID,Level等字段,下面要實現的就是從一(yi)級(ji)節點(dian)開始,一(yi)級(ji)一(yi)級(ji)的列出來,并以
下拉列表框的形(xing)式體現出來,就(jiu)像是N級(ji)聯動。
效果圖:
兩個問題:
1 建立操作時的聯動,它不需要進行自動綁定(ding)
2 編輯操作時的(de)聯運,它需(xu)要(yao)根據子(zi)節點,逐級自己綁定到(dao)父節點,直到(dao)根
實現:
JS代碼
1 <script type="text/javascript"> 2 function areaOnSelect(obj) { 3 var res = ''; 4 $.ajax({ url: '@Url.Action("GetSubTree")', 5 type: 'GET', 6 data: { parentId: obj.value }, 7 success: function (msg) { 8 $(obj).nextAll().remove(); 9 res = "<select name='Sub' onchange='areaOnSelect(this)'>"; 10 res += "<option value=''>請選擇</option>"; 11 $.each(msg, function (i, item) { 12 res += "<option value='" + item["ID"] + "'>" + item["Name"] + "</option>"; 13 }); 14 res += "</select>"; 15 if ($(res).find("option").size() > 1) 16 $(obj).after(res); 17 } 18 }); 19 } 20 </script>
C#代碼:
1 #region 樹型結(jie)構(gou)相(xiang)關 2 /// <summary> 3 /// 遞歸找老(lao)祖宗 4 /// </summary> 5 /// <param name="father"></param> 6 void GetFather(SubItem father) 7 { 8 if (father != null) 9 { 10 father.Parent = _subList.FirstOrDefault(i => i.ID == father.ParentID); 11 GetFather(father.Parent); 12 } 13 } 14 15 /// <summary> 16 /// 弟妹找子孫 17 /// </summary> 18 /// <param name="father">父對(dui)象(xiang)</param> 19 void getSons(SubItem father) 20 { 21 if (father != null) 22 { 23 father.Sons = _subList.Where(item => 24 item.ParentID.Equals(father.ID)).ToList(); 25 father.Sons.ForEach(item => 26 { 27 item.Parent = father; 28 getSons(item); 29 }); 30 } 31 } 32 33 34 #endregion
C#拼接下拉(la)列表(biao)框相關:
1 /// <summary> 2 /// 遞(di)歸得(de)到(dao)它的所有祖宗以selectlist的形(xing)式進(jin)行拼接 3 /// </summary> 4 /// <param name="son"></param> 5 /// <param name="sbr"></param> 6 void getSelectList(SubItem son, StringBuilder sbr) 7 { 8 StringBuilder inSbr = new StringBuilder(); 9 if (son != null) 10 { 11 if (son.ParentID == 0) 12 inSbr.Append("<select name='Parent' onchange = 'areaOnSelect(this)' >"); 13 else 14 inSbr.Append("<select name='Sub'>"); 15 GetCommon_CategoryByLevel(son.Level).ToList().ForEach(i => 16 { 17 if (i.ID == son.ID) 18 inSbr.Append("<option value='" + i.ID + "' selected='true'>" + i.Name + "</option>"); 19 else 20 inSbr.Append("<option value='" + i.ID + "'>" + i.Name + "</option>"); 21 }); 22 23 inSbr.Append("</select>"); 24 sbr.Insert(0, inSbr); 25 getSelectList(son.Parent, sbr); 26 } 27 }
C#得(de)到同(tong)一深度的節點(同(tong)輩節點)相關:
1 /// <summary> 2 /// 得到(dao)指(zhi)定深度(du)的列表(biao) 3 /// </summary> 4 /// <param name="level"></param> 5 /// <returns></returns> 6 public List<SubItem> GetCommon_CategoryByLevel(int level) 7 { 8 var linq = from data1 in _subList 9 join data2 in _subList on data1.ParentID equals data2.ID into list 10 select new SubItem 11 { 12 ID = data1.ID, 13 Level = data1.Level, 14 Name = data1.Name, 15 Parent = list.FirstOrDefault(), 16 ParentID = data1.ParentID, 17 }; 18 return linq.Where(i => i.Level.Equals(level)).ToList(); 19 }
MVC頁(ye)面action相關(guan):
1 public ActionResult Category(int? id) 2 { 3 ViewData["Parent"] = new SelectList(_subList.Where(i => i.ID == (id ?? 0)), "ID", "Name", id ?? 1); 4 SubItem current = _subList.FirstOrDefault(i => i.ID == (id ?? 1)); 5 GetFather(current); 6 StringBuilder sbr = new StringBuilder(); 7 getSelectList(current, sbr); 8 ViewData["edit"] = sbr.ToString();//修(xiu)改時(shi),進行綁定 9 return View(); 10 }
MVC頁面代碼相關(guan):
1 @Html.Raw(ViewData["edit"].ToString())
C#樹(shu)型結構(gou)實體類相關:
1 /// <summary> 2 /// 樹型(xing)分(fen)類結構 3 /// </summary> 4 public class Category 5 { 6 /// <summary> 7 /// 父(fu)ID 8 /// </summary> 9 public int ParentID { get; set; } 10 /// <summary> 11 /// 樹ID 12 /// </summary> 13 public int ID { get; set; } 14 /// <summary> 15 /// 樹名稱 16 /// </summary> 17 public string Name { get; set; } 18 /// <summary> 19 /// 深度 20 /// </summary> 21 public int Level { get; set; } 22 /// <summary> 23 /// 子孫節點 24 /// </summary> 25 public List<Category> Sons { get; set; } 26 /// <summary> 27 /// 父節點 28 /// </summary> 29 public Category Parent { get; set; } 30 }
好了,現在(zai)我們的N級無限下(xia)拉列表框就(jiu)做好了,呵呵!