C#完成無窮級聯下拉列表框。本站提示廣大學習愛好者:(C#完成無窮級聯下拉列表框)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成無窮級聯下拉列表框正文
本文實例為年夜家分享了無窮級聯下拉列表框的的完成辦法,詳細內容以下
能夠有一個樹型構造的表,它能夠有ID,Name,ParentID,Level等字段,上面要完成的就是從一級節點開端,一級一級的列出來,並以
下拉列表框的情勢表現出來,就像是N級聯動。
後果圖:

兩個成績:
1、樹立操作時的聯動,它不須要停止主動綁定
2、編纂操作時的聯運,它須要依據子節點,逐級本身綁定到父節點,直到根
完成:
JS代碼
<script type="text/javascript">
function areaOnSelect(obj) {
var res = '';
$.ajax({ url: '@Url.Action("GetSubTree")',
type: 'GET',
data: { parentId: obj.value },
success: function (msg) {
$(obj).nextAll().remove();
res = "<select name='Sub' onchange='areaOnSelect(this)'>";
res += "<option value=''>請選擇</option>";
$.each(msg, function (i, item) {
res += "<option value='" + item["ID"] + "'>" + item["Name"] + "</option>";
});
res += "</select>";
if ($(res).find("option").size() > 1)
$(obj).after(res);
}
});
}
</script>
C#代碼:
#region 樹型構造相干
/// <summary>
/// 遞歸找老祖宗
/// </summary>
/// <param name="father"></param>
void GetFather(SubItem father)
{
if (father != null)
{
father.Parent = _subList.FirstOrDefault(i => i.ID == father.ParentID);
GetFather(father.Parent);
}
}
/// <summary>
/// 弟妹找子孫
/// </summary>
/// <param name="father">父對象</param>
void getSons(SubItem father)
{
if (father != null)
{
father.Sons = _subList.Where(item =>
item.ParentID.Equals(father.ID)).ToList();
father.Sons.ForEach(item =>
{
item.Parent = father;
getSons(item);
});
}
}
#endregion
C#拼接下拉列表框相干:
/// <summary>
/// 遞歸獲得它的一切祖宗以selectlist的情勢停止拼接
/// </summary>
/// <param name="son"></param>
/// <param name="sbr"></param>
void getSelectList(SubItem son, StringBuilder sbr)
{
StringBuilder inSbr = new StringBuilder();
if (son != null)
{
if (son.ParentID == 0)
inSbr.Append("<select name='Parent' onchange = 'areaOnSelect(this)' >");
else
inSbr.Append("<select name='Sub'>");
GetCommon_CategoryByLevel(son.Level).ToList().ForEach(i =>
{
if (i.ID == son.ID)
inSbr.Append("<option value='" + i.ID + "' selected='true'>" + i.Name + "</option>");
else
inSbr.Append("<option value='" + i.ID + "'>" + i.Name + "</option>");
});
inSbr.Append("</select>");
sbr.Insert(0, inSbr);
getSelectList(son.Parent, sbr);
}
}
C#獲得統一深度的節點(平輩節點)相干:
/// <summary>
/// 獲得指定深度的列表
/// </summary>
/// <param name="level"></param>
/// <returns></returns>
public List<SubItem> GetCommon_CategoryByLevel(int level)
{
var linq = from data1 in _subList
join data2 in _subList on data1.ParentID equals data2.ID into list
select new SubItem
{
ID = data1.ID,
Level = data1.Level,
Name = data1.Name,
Parent = list.FirstOrDefault(),
ParentID = data1.ParentID,
};
return linq.Where(i => i.Level.Equals(level)).ToList();
}
MVC頁面action相干:
public ActionResult Category(int? id)
{
ViewData["Parent"] = new SelectList(_subList.Where(i => i.ID == (id ?? 0)), "ID", "Name", id ?? 1);
SubItem current = _subList.FirstOrDefault(i => i.ID == (id ?? 1));
GetFather(current);
StringBuilder sbr = new StringBuilder();
getSelectList(current, sbr);
ViewData["edit"] = sbr.ToString();//修正時,停止綁定
return View();
}
MVC頁面代碼相干:
@Html.Raw(ViewData["edit"].ToString())
C#樹型構造實體類相干:
/// <summary>
/// 樹型分類構造
/// </summary>
public class Category
{
/// <summary>
/// 父ID
/// </summary>
public int ParentID { get; set; }
/// <summary>
/// 樹ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 樹稱號
/// </summary>
public string Name { get; set; }
/// <summary>
/// 深度
/// </summary>
public int Level { get; set; }
/// <summary>
/// 子孫節點
/// </summary>
public List<Category> Sons { get; set; }
/// <summary>
/// 父節點
/// </summary>
public Category Parent { get; set; }
}
好了,如今我們的N級無窮下拉列表框就做好了,感激年夜家的浏覽。