欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

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

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

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

效果圖:

兩個(gè)問(wèn)題:

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

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

實(shí)現(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=''>請(qǐng)選擇</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 樹(shù)型結(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">父對(duì)象</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é)點(diǎn)(同輩節(jié)點(diǎn))相關(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頁(yè)面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();//修改時(shí),進(jìn)行綁定
      return View();
    }

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

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

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

/// <summary>
  /// 樹(shù)型分類結(jié)構(gòu)
  /// </summary>
  public class Category
  {
    /// <summary>
    /// 父ID
    /// </summary>
    public int ParentID { get; set; }
    /// <summary>
    /// 樹(shù)ID
    /// </summary>
    public int ID { get; set; }
    /// <summary>
    /// 樹(shù)名稱
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// 深度
    /// </summary>
    public int Level { get; set; }
    /// <summary>
    /// 子孫節(jié)點(diǎn)
    /// </summary>
    public List<Category> Sons { get; set; }
    /// <summary>
    /// 父節(jié)點(diǎn)
    /// </summary>
    public Category Parent { get; set; }
  }

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

上一篇:C# WCF簡(jiǎn)單入門圖文教程(VS2010版)

欄    目:C#教程

下一篇:C#簡(jiǎn)單郵件群發(fā)通用類

本文標(biāo)題:C#實(shí)現(xiàn)無(wú)限級(jí)聯(lián)下拉列表框

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6642.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有