带线的无限级下拉树列表

简介:

好多年没写文章了
这里就分享点自己原创的一点破代码,效果如图下:
r_%7BDB83491B-1991-48D4-9A9B-69704E20B45
本人的提供的代码如下:

None.gif using System;
None.gif using System.Collections.Generic;
None.gif using System.Text;
None.gif using System.Web.UI.WebControls;
None.gif
None.gif namespace Interface.Common
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gif    public interface IDropDownTree : IDisposable
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 返回Dictionary里分别对应ID,文本,如果没有子节点返回null
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="parentID">父节点ID</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        Dictionary<stringstring> GetChildCategory(string parentID);
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 代码里写return new Interface.Common.DropDownTree(this);
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        DropDownTree DropDownTree
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            get;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    public sealed class DropDownTree
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        IDropDownTree _DropDownTree;
InBlock.gif        public DropDownTree(IDropDownTree dropDownTree)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            _DropDownTree = dropDownTree;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 用于树的前缀
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="IsLast">是否是同级节点中的最后一个</param>
InBlock.gif        
/// <param name="HasChild">本节点是否拥有子节点</param>
InBlock.gif        
/// <param name="ParentString">父节点前缀符号</param>
ExpandedSubBlockEnd.gif        
/// <returns>本节点的前缀</returns>

InBlock.gif        private string GetPreFix(bool isLast, bool hasChild, string parentString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string result = string.Empty;
InBlock.gif            if (!string.IsNullOrEmpty(parentString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                parentString = parentString.Remove(parentString.Length - 1).Replace("""").Replace(""" ");
InBlock.gif                result += parentString;
ExpandedSubBlockEnd.gif            }

InBlock.gif            if (isLast)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                result += "";
ExpandedSubBlockEnd.gif            }

InBlock.gif            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                result += "";
ExpandedSubBlockEnd.gif            }

InBlock.gif            if (hasChild)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                result += "";
ExpandedSubBlockEnd.gif            }

InBlock.gif            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                result += "";
ExpandedSubBlockEnd.gif            }

InBlock.gif            return result;
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        绑定下拉菜单#region 绑定下拉菜单
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 绑定连动级的下拉菜单
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
InBlock.gif        
/// <param name="removeID">被排除绑定的节点ID</param>
ExpandedSubBlockEnd.gif        
/// <param name="AutoDispose">是否自动释放</param>

InBlock.gif        public void BindToDropDownList(DropDownList ddlGoodsType, string removeID,string parentID, bool autoDispose)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            if (ddlGoodsType != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                ListItem listItem = null;
InBlock.gif                string currentID = parentID;//根节点/父ID
InBlock.gif
                string currentSign = string.Empty;//当前节点符号;
InBlock.gif
                string parrentSign = string.Empty; //父节点符号;
InBlock.gif
                bool HasChild = true;//是否有子
InBlock.gif
                Queue<string> parentKeyList = new Queue<string>();//存 有子节点的 节点ID
InBlock.gif
                Queue<string> parentSignList = new Queue<string>();//对应节点ID的前缀符号
InBlock.gif
                int itemIndexOf = 0;//父节点所在的位置 
InBlock.gif
                while (HasChild)
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    int lastOneCount = 1;//用于计算在同级别中是否最后一个
InBlock.gif
                    Dictionary<stringstring> childList = _DropDownTree.GetChildCategory(currentID);// 得到子节点列表
InBlock.gif
                    if (childList != null && childList.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
InBlock.gif                        if (!string.IsNullOrEmpty(removeID) && childList.ContainsKey(removeID))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
InBlock.gif                            childList.Remove(removeID);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        foreach (KeyValuePair<stringstring> entry in childList)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
InBlock.gif                            if (_DropDownTree.GetChildCategory(entry.Key) != null)//存在子
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif                                currentSign = GetPreFix(lastOneCount == childList.Count, true, parrentSign);
InBlock.gif                                listItem = new ListItem(currentSign + entry.Value, entry.Key);
InBlock.gif
InBlock.gif                                parentKeyList.Enqueue(entry.Key);//当前的节点ID
InBlock.gif
                                parentSignList.Enqueue(currentSign);//当前的节点符号
ExpandedSubBlockEnd.gif
                            }

InBlock.gif                            else//不存在子
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif                                currentSign = GetPreFix(lastOneCount == childList.Count, false, parrentSign);
InBlock.gif                                listItem = new ListItem(currentSign + entry.Value, entry.Key);
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            if (ddlGoodsType.Items.Count != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
InBlock.gif                                itemIndexOf = string.IsNullOrEmpty(currentID) ? itemIndexOf + 1 : ddlGoodsType.Items.IndexOf(ddlGoodsType.Items.FindByValue(currentID)) + lastOneCount;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            ddlGoodsType.Items.Insert(itemIndexOf, listItem);//添加子节点
InBlock.gif
                            lastOneCount++;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        if (parentKeyList.Count > 0)//存在子节点时
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            currentID = parentKeyList.Dequeue();
InBlock.gif                            parrentSign = parentSignList.Dequeue();
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
InBlock.gif                            HasChild = false;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
InBlock.gif                        break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                if (autoDispose)
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    _DropDownTree.Dispose();
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 绑定连动级的下拉菜单
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>

InBlock.gif        public void BindToDropDownList(DropDownList ddlGoodsType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            BindToDropDownList(ddlGoodsType, string.Empty,nulltrue);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 绑定连动级的下拉菜单
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
ExpandedSubBlockEnd.gif        
/// <param name="removeID">被排除的ID</param>

InBlock.gif        public void BindToDropDownList(DropDownList ddlGoodsType, string removeID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            BindToDropDownList(ddlGoodsType, removeID,nulltrue);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 绑定连动级的下拉菜单
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ddlGoodsType">传进一个被绑定的DropDownList</param>
InBlock.gif        
/// <param name="removeID">被排除的ID,若没有,传null</param>
ExpandedSubBlockEnd.gif        
/// <param name="parentID">起始父ID</param>

InBlock.gif        public void BindToDropDownList(DropDownList ddlGoodsType, string removeID,string parentID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            BindToDropDownList(ddlGoodsType, removeID,parentID, true);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        #endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


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

实现接口代码示例[Dispose方法自己实现],最主要的是自己实现获得子级的方法
ContractedBlock.gif ExpandedBlockStart.gif  IDropDownTree 成员 #region IDropDownTree 成员
InBlock.gif
InBlock.gif        public Dictionary<stringstring> GetChildCategory(string parentID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string where = "ParentID='" + parentID + "'";
InBlock.gif            if (string.IsNullOrEmpty(parentID))
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                where = "ParentID is null or ParentID='" + Guid.Empty + "'";
ExpandedSubBlockEnd.gif            }

InBlock.gif            List<GoodsCategoryBean> _GoodsCategoryList = SelectList(0wherestring.Empty, false);
InBlock.gif            if (_GoodsCategoryList != null && _GoodsCategoryList.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                Dictionary<stringstring> categoryList = new Dictionary<stringstring>();
InBlock.gif                for (int i = 0; i < _GoodsCategoryList.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    categoryList.Add(_GoodsCategoryList[i].ID.ToString(), _GoodsCategoryList[i].GategoryName);
ExpandedSubBlockEnd.gif                }

InBlock.gif                return categoryList;
ExpandedSubBlockEnd.gif            }

InBlock.gif            return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        public Interface.Common.DropDownTree DropDownTree
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn new Interface.Common.DropDownTree(this); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        #endregion

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

希望对大伙有点帮助....
相关文章
|
11天前
【sgSearch】自定义组件:常用搜索栏筛选框组件(包括表格高度变化兼容)。
【sgSearch】自定义组件:常用搜索栏筛选框组件(包括表格高度变化兼容)。
|
8月前
|
前端开发
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序4
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序4
32 0
|
6月前
|
人工智能 自然语言处理 语音技术
elementUI使用v-for创建无限级导航栏—— 递归组件
elementUI使用v-for创建无限级导航栏—— 递归组件
49 0
|
6月前
|
敏捷开发 前端开发 开发者
【RaETable】🚀🚀🚀告别Form,RaETable表格列宽度支持拖动调整了,附带原理说明
【RaETable】🚀🚀🚀告别Form,RaETable表格列宽度支持拖动调整了,附带原理说明
|
7月前
|
设计模式 人工智能 安全
没有性能瓶颈的无限级菜单树应该这样设计
以一门网络课程为例,我们设计一个课程的关系结构。比如,我们有Java入门课程、人工智能课程、Java设计模式、源码分析、软技能等,而Java设计模式、源码分析、软技能又属于Java架构师系列课程包,每个课程的定价都不一样。但是,这些课程不论怎么组合,都有一些共性,而且是整体和部分的关系,可以用组合模式来设计。首先创建一个顶层的抽象组件CourseComponent类。
61 0
|
8月前
|
前端开发
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序3
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序3
33 0
|
8月前
|
前端开发
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序2
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序2
33 0
|
8月前
|
前端开发
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序1
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序1
31 0
|
8月前
|
Web App开发 缓存 JavaScript
简洁、巧妙、高效的长列表,无限下拉方案
简洁、巧妙、高效的长列表,无限下拉方案
74 0