首页 > 其他分享 >带线的无限级下拉树列表

带线的无限级下拉树列表

时间:2022-09-29 23:41:04浏览次数:45  
标签:DropDownTree string 下拉树 null 列表 ID 带线 节点 ddlGoodsType

好多年没写文章了

这里就分享点自己原创的一点破代码,效果如图下:


本人的提供的代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace Interface.Common
{
public interface IDropDownTree : IDisposable

{
/**//// <summary>
/// 返回Dictionary里分别对应ID,文本,如果没有子节点返回null
/// </summary>
/// <param name="parentID">父节点ID</param>
/// <returns></returns>
Dictionary<string, string> GetChildCategory(string parentID);
/**//// <summary>
/// 代码里写return new Interface.Common.DropDownTree(this);
/// </summary>
DropDownTree DropDownTree

{
get;
}
}
public sealed class DropDownTree

{
IDropDownTree _DropDownTree;
public DropDownTree(IDropDownTree dropDownTree)

{
_DropDownTree = dropDownTree;
}
/**//// <summary>
/// 用于树的前缀
/// </summary>
/// <param name="IsLast">是否是同级节点中的最后一个</param>
/// <param name="HasChild">本节点是否拥有子节点</param>
/// <param name="ParentString">父节点前缀符号</param>
/// <returns>本节点的前缀</returns>
private string GetPreFix(bool isLast, bool hasChild, string parentString)

{
string result = string.Empty;
if (!string.IsNullOrEmpty(parentString))

{
parentString = parentString.Remove(parentString.Length - 1).Replace("├", "│").Replace("└", " ");
result += parentString;
}
if (isLast)

{
result += "└";
}
else

{
result += "├";
}
if (hasChild)

{
result += "┬";
}
else

{
result += "─";
}
return result;
}
绑定下拉菜单#region 绑定下拉菜单

/**//// <summary>
/// 绑定连动级的下拉菜单
/// </summary>
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
/// <param name="removeID">被排除绑定的节点ID</param>
/// <param name="AutoDispose">是否自动释放</param>
public void BindToDropDownList(DropDownList ddlGoodsType, string removeID,string parentID, bool autoDispose)

{
if (ddlGoodsType != null)

{
ListItem listItem = null;
string currentID = parentID;//根节点/父ID
string currentSign = string.Empty;//当前节点符号;
string parrentSign = string.Empty; //父节点符号;
bool HasChild = true;//是否有子
Queue<string> parentKeyList = new Queue<string>();//存 有子节点的 节点ID
Queue<string> parentSignList = new Queue<string>();//对应节点ID的前缀符号
int itemIndexOf = 0;//父节点所在的位置 
while (HasChild)

{
int lastOneCount = 1;//用于计算在同级别中是否最后一个
Dictionary<string, string> childList = _DropDownTree.GetChildCategory(currentID);// 得到子节点列表
if (childList != null && childList.Count > 0)

{
if (!string.IsNullOrEmpty(removeID) && childList.ContainsKey(removeID))

{
childList.Remove(removeID);
}
foreach (KeyValuePair<string, string> entry in childList)

{
if (_DropDownTree.GetChildCategory(entry.Key) != null)//存在子

{
currentSign = GetPreFix(lastOneCount == childList.Count, true, parrentSign);
listItem = new ListItem(currentSign + entry.Value, entry.Key);

parentKeyList.Enqueue(entry.Key);//当前的节点ID
parentSignList.Enqueue(currentSign);//当前的节点符号
}
else//不存在子

{
currentSign = GetPreFix(lastOneCount == childList.Count, false, parrentSign);
listItem = new ListItem(currentSign + entry.Value, entry.Key);
}
if (ddlGoodsType.Items.Count != 0)

{
itemIndexOf = string.IsNullOrEmpty(currentID) ? itemIndexOf + 1 : ddlGoodsType.Items.IndexOf(ddlGoodsType.Items.FindByValue(currentID)) + lastOneCount;
}
ddlGoodsType.Items.Insert(itemIndexOf, listItem);//添加子节点
lastOneCount++;
}
if (parentKeyList.Count > 0)//存在子节点时

{
currentID = parentKeyList.Dequeue();
parrentSign = parentSignList.Dequeue();
}
else

{
HasChild = false;
}
}
else

{
break;
}


}
if (autoDispose)

{
_DropDownTree.Dispose();
}

}
}
/**//// <summary>
/// 绑定连动级的下拉菜单
/// </summary>
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
public void BindToDropDownList(DropDownList ddlGoodsType)

{
BindToDropDownList(ddlGoodsType, string.Empty,null, true);
}
/**//// <summary>
/// 绑定连动级的下拉菜单
/// </summary>
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
/// <param name="removeID">被排除的ID</param>
public void BindToDropDownList(DropDownList ddlGoodsType, string removeID)

{
BindToDropDownList(ddlGoodsType, removeID,null, true);
}
/**//// <summary>
/// 绑定连动级的下拉菜单
/// </summary>
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
/// <param name="removeID">被排除的ID,若没有,传null</param>
/// <param name="parentID">起始父ID</param>
public void BindToDropDownList(DropDownList ddlGoodsType, string removeID,string parentID)

{
BindToDropDownList(ddlGoodsType, removeID,parentID, true);
}
#endregion
}
}

调用方法很简单:
1.继承自IDropDownTree接口
2.实现3个接口方法

实现接口代码示例[Dispose方法自己实现],最主要的是自己实现获得子级的方法
IDropDownTree 成员#region IDropDownTree 成员

public Dictionary<string, string> GetChildCategory(string parentID)

{
string where = "ParentID='" + parentID + "'";
if (string.IsNullOrEmpty(parentID))

{
where = "ParentID is null or ParentID='" + Guid.Empty + "'";
}
List<GoodsCategoryBean> _GoodsCategoryList = SelectList(0, where, string.Empty, false);
if (_GoodsCategoryList != null && _GoodsCategoryList.Count > 0)

{
Dictionary<string, string> categoryList = new Dictionary<string, string>();
for (int i = 0; i < _GoodsCategoryList.Count; i++)

{
categoryList.Add(_GoodsCategoryList[i].ID.ToString(), _GoodsCategoryList[i].GategoryName);
}
return categoryList;
}
return null;
}

public Interface.Common.DropDownTree DropDownTree

{
get
{ return new Interface.Common.DropDownTree(this); }
}

#endregion

页面调用代码: 类名.DropDownTree.BindToDropDownList(下拉控件ID);


希望对大伙有点帮助....

标签:DropDownTree,string,下拉树,null,列表,ID,带线,节点,ddlGoodsType
From: https://blog.51cto.com/cyq1162/5724427

相关文章

  • python如何依次打印出列表中的元素
    list1=[]forjinrange(1,100):list1.append(j)list2=[]sum1=int(input("输入一个数字:"))#定义一个int类型i=0while(i<=len(list1)):#对输入......
  • 列表内置方法及操作
    列表内置方法及操作一、类型转换list(其他数据类型)把其他数据类型转成列表print(type(list('123')),list('123'))#<class'list'>['1','2','3']print(type(l......
  • 列表内置方法及操作
    今日内容数据类型的内置方法理论我们之前所学习的每一种数据类型本身都含有一些列的操作方法内置方法是其中最多的(自带功能)在python中数据类型调用内置方法的同意句......
  • python实现找到列表中第二大的数字
    '''给定一个长度大于3的列表,里面数字是无序的,且数字不重复,如何找到第二大的数字?例如列表a=[1,3,6,2,7,9],找到的结果就应该是:7下面给出六种解决方案'''a=[1,3,6......
  • Vue 超长列表渲染性能优化
    参考:https://juejin.cn/post/6979865534166728711#heading-3组件懒加载参考:https://github.com/xunleif2e/vue-lazy-component......
  • Element UI下拉列表el-option中的key、value、label含义
    ElementUI下拉列表el-option中的key、value、label含义<el-selectv-model="queryParams.level"placeholder="级别"style='margin-right:5px;width:140px'clearable>......
  • Python元组常用方法 || 元组和列表的区别
    Python元组常用方法前言①通过()创建元组。小括号可以省略。a=(10,20,30)或者a=10,20,30【注意】:如果元组只有一个元素,则必须后面加逗号。这是因为python解释器会把 ......
  • 10. HTML-- 列表标签:<ul><ol><dl>
    1.前言HTML列表(List)可以将若干条相关的内容整理起来,让内容看起来更加有条理。在列表内您可以放置​​文本、图像、链接等,也可以在一个列表中定义另一个列表(列表嵌套)。HT......
  • C# 两个列表取交集并集差集,比较复杂对象列表
    publicclassPosSaleReportDataShopComparer:IEqualityComparer<PosSaleReportData>{privatedoubledayDiff=365;publicPosSaleReportDataShopComparer(do......
  • js 获取当前地址的查询参数列表
    eg.https://go.gliffy.com/go/html5/launch?_ga=2.201967958.654328489.1658124867-1818406430.1658124867console.log(location.search)结果:?_ga=2.201967958.654328489.16......