带线的无限级下拉树列表-阿里云开发者社区

开发者社区> 云计算> 正文
登录阅读全文

带线的无限级下拉树列表

简介:

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

None.gifusing System;
None.gifusing System.Collections.Generic;
None.gifusing System.Text;
None.gifusing System.Web.UI.WebControls;
None.gif
None.gifnamespace Interface.Common
ExpandedBlockStart.gif{
InBlock.gif    public interface IDropDownTree : IDisposable
ExpandedSubBlockStart.gif    {
ExpandedSubBlockStart.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.gif        /// <summary>
InBlock.gif        
/// 代码里写return new Interface.Common.DropDownTree(this);
ExpandedSubBlockEnd.gif        
/// </summary>

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

ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockStart.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.gif        {
InBlock.gif            string result = string.Empty;
InBlock.gif            if (!string.IsNullOrEmpty(parentString))
ExpandedSubBlockStart.gif            {
InBlock.gif                parentString = parentString.Remove(parentString.Length - 1).Replace("""").Replace(""" ");
InBlock.gif                result += parentString;
ExpandedSubBlockEnd.gif            }

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

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

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

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

InBlock.gif            return result;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gif        #region 绑定下拉菜单
InBlock.gif
ExpandedSubBlockStart.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.gif        {
InBlock.gif            if (ddlGoodsType != null)
ExpandedSubBlockStart.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.gif                {
InBlock.gif                    int lastOneCount = 1;//用于计算在同级别中是否最后一个
InBlock.gif
                    Dictionary<stringstring> childList = _DropDownTree.GetChildCategory(currentID);// 得到子节点列表
InBlock.gif
                    if (childList != null && childList.Count > 0)
ExpandedSubBlockStart.gif                    {
InBlock.gif                        if (!string.IsNullOrEmpty(removeID) && childList.ContainsKey(removeID))
ExpandedSubBlockStart.gif                        {
InBlock.gif                            childList.Remove(removeID);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        foreach (KeyValuePair<stringstring> entry in childList)
ExpandedSubBlockStart.gif                        {
InBlock.gif                            if (_DropDownTree.GetChildCategory(entry.Key) != null)//存在子
ExpandedSubBlockStart.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.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.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.gif
                        {
InBlock.gif                            currentID = parentKeyList.Dequeue();
InBlock.gif                            parrentSign = parentSignList.Dequeue();
ExpandedSubBlockEnd.gif                        }

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

ExpandedSubBlockEnd.gif                    }

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

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                }

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

InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

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

ExpandedSubBlockStart.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.gif        {
InBlock.gif            BindToDropDownList(ddlGoodsType, removeID,nulltrue);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.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.gif        {
InBlock.gif            BindToDropDownList(ddlGoodsType, removeID,parentID, true);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        #endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


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

实现接口代码示例[Dispose方法自己实现],最主要的是自己实现获得子级的方法
ExpandedBlockStart.gif #region IDropDownTree 成员
InBlock.gif
InBlock.gif        public Dictionary<stringstring> GetChildCategory(string parentID)
ExpandedSubBlockStart.gif        {
InBlock.gif            string where = "ParentID='" + parentID + "'";
InBlock.gif            if (string.IsNullOrEmpty(parentID))
ExpandedSubBlockStart.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.gif            {
InBlock.gif                Dictionary<stringstring> categoryList = new Dictionary<stringstring>();
InBlock.gif                for (int i = 0; i < _GoodsCategoryList.Count; i++)
ExpandedSubBlockStart.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.gif        {
ExpandedSubBlockStart.gif            get return new Interface.Common.DropDownTree(this); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        #endregion

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

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: