国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現(xiàn)無限級聯(lián)下拉列表框

2020-01-24 01:14:36
字體:
供稿:網(wǎng)友

本文實例為大家分享了無限級聯(lián)下拉列表框的的實現(xiàn)方法,具體內(nèi)容如下

可能有一個樹型結(jié)構(gòu)的表,它可能有ID,Name,ParentID,Level等字段,下面要實現(xiàn)的就是從一級節(jié)點開始,一級一級的列出來,并以

下拉列表框的形式體現(xiàn)出來,就像是N級聯(lián)動。

效果圖:

兩個問題:

1、建立操作時的聯(lián)動,它不需要進(jìn)行自動綁定

2、編輯操作時的聯(lián)運(yùn),它需要根據(jù)子節(jié)點,逐級自己綁定到父節(jié)點,直到根

實現(xiàn):

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 樹型結(jié)構(gòu)相關(guān)    /// <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#拼接下拉列表框相關(guān):

/// <summary>    /// 遞歸得到它的所有祖宗以selectlist的形式進(jìn)行拼接    /// </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#得到同一深度的節(jié)點(同輩節(jié)點)相關(guān):

/// <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相關(guān):

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();//修改時,進(jìn)行綁定      return View();    }

MVC頁面代碼相關(guān):

 @Html.Raw(ViewData["edit"].ToString())

C#樹型結(jié)構(gòu)實體類相關(guān):

/// <summary>  /// 樹型分類結(jié)構(gòu)  /// </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>    /// 子孫節(jié)點    /// </summary>    public List<Category> Sons { get; set; }    /// <summary>    /// 父節(jié)點    /// </summary>    public Category Parent { get; set; }  }

好了,現(xiàn)在我們的N級無限下拉列表框就做好了,感謝大家的閱讀。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥滨县| 三台县| 芜湖县| 香格里拉县| 太仓市| 西林县| 志丹县| 伊吾县| 宜章县| 郎溪县| 盱眙县| 明星| 平顶山市| 个旧市| 富源县| 常熟市| 岱山县| 布拖县| 垣曲县| 施秉县| 津南区| 娄烦县| 闸北区| 东阿县| 江永县| 峡江县| 内黄县| 广丰县| 手机| 祥云县| 普兰店市| 崇仁县| 革吉县| 合江县| 溧水县| 新野县| 江陵县| 永安市| 屯昌县| 贺州市| 桐乡市|