C#实现无限级联下拉列表框
程序员文章站
2022-11-24 07:55:57
本文实例为大家分享了无限级联下拉列表框的的实现方法,具体内容如下
可能有一个树型结构的表,它可能有id,name,parentid,level等字段,下面要实现的就是从一...
本文实例为大家分享了无限级联下拉列表框的的实现方法,具体内容如下
可能有一个树型结构的表,它可能有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级无限下拉列表框就做好了,感谢大家的阅读。