亲宝软件园·资讯

展开

C#实现无限级联下拉列表框 C#实现无限级联下拉列表框

人气:0
想了解C#实现无限级联下拉列表框的相关内容吗,在本文为您仔细讲解C#实现无限级联下拉列表框的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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级无限下拉列表框就做好了,感谢大家的阅读。

加载全部内容

相关教程
猜你喜欢
用户评论