开发者社区> 路过秋天> 正文

带线的无限级下拉树列表

简介:
+关注继续查看

好多年没写文章了
这里就分享点自己原创的一点破代码,效果如图下:
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.gifContractedBlock.gifdot.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.gifExpandedBlockStart.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);

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

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

相关文章
【自然框架】n级下拉列表框的原理
  其实原理也很简单,分成两个部分,一个是服务器端,一个是客户端。     首先要设置记录集,这里用DataSet来装载,二级联动,里面就要有两个DataTable;三级联动,里面就要有三个DataTable。
1038 0
关于树标签的问题1-动静模式是,点击下级节点不能展开的问题
关于树标签的问题-动静模式是,点击下级节点不能展开的问题 bboss 项目文件清单:https://sourceforge.net/projects/bboss/files/ bboss taglib 1.
492 0
20个优秀的固定位置网站菜单设计案例
  导航菜单作为网站必不可少的组成部分,关系着网站的可用性和用户体验。有吸引力的导航能够吸引用户去浏览更多的网站内容,增加用户在网站的停留时间。常见的导航形式有头部导航、侧栏导航、底部导航和固定位置导航等各种形式,今天这篇文章要和大家分享的是20佳国外优秀的固定位置导航菜单设计案例,相信这些形式各异的导航菜单设计能够带给你灵感。
642 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
动态、高效,蚂蚁动态卡片的内核逻辑
立即下载
数据带来无限可能
立即下载
图计算优化技术探索
立即下载