带线的无限级下拉树列表-完整示例篇
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
前言:
今天在群里有人問起了我一個比較遠(yuǎn)古的問題:帶線的無限級下拉樹列表他運行不起來。最關(guān)鍵的又扯上了CYQ.Data 框架,讓我一時覺得比較懸,因為文章是08年時寫的,而框架最今年才發(fā)力完善的,
所以兩者應(yīng)該是沒啥聯(lián)系的,不過這一問也好,給了我一個寫此文章的機(jī)會。
ps:他把示例的其它代碼當(dāng)成是?CYQ.Data 框架?里的代碼。
?
?
本文將對之前的代碼進(jìn)行小小的簡化,并為之建立一個完整的應(yīng)用示例,以下為正式應(yīng)用步驟:
?
一:新建項目
1:將IDropDownTree及DropDownTree放到類里,如下圖:
簡化后的代碼如下:
IDropDownTree:
IDropDownTree using ?System.Collections.Generic; using?System;namespace ?Tree
{
???? public ? interface ?IDropDownTree?:?IDisposable
????{
???????? /// ? <summary>
???????? /// ?返回Dictionary里分別對應(yīng)ID,文本,如果沒有子節(jié)點返回null
???????? /// ? </summary>
???????? /// ? <param?name="parentID"> 父節(jié)點ID </param>
???????? /// ? <returns></returns>
????????Dictionary < string ,? string > ?GetChildList( string ?parentID);
???????? /// ? <summary>
???????? /// ?實現(xiàn)的代碼里寫return?new?Tree.DropDownTree(this);
???????? /// ? </summary>
????????DropDownTree?DropDownTree
????????{
???????????? get ;
????????}
????}
}
?
DropDownTree:
DropDownTree using ?System.Collections.Generic;using ?System.Web.UI.WebControls;
namespace ?Tree
{
???? public ? sealed ? class ?DropDownTree
????{
????????IDropDownTree?_DropDownTree;
???????? public ?DropDownTree(IDropDownTree?dropDownTree)
????????{
????????????_DropDownTree? = ?dropDownTree;
????????}
???????? /// ? <summary>
???????? /// ?用于樹的前綴
???????? /// ? </summary>
???????? /// ? <param?name="IsLast"> 是否是同級節(jié)點中的最后一個 </param>
???????? /// ? <param?name="HasChild"> 本節(jié)點是否擁有子節(jié)點 </param>
???????? /// ? <param?name="ParentString"> 父節(jié)點前綴符號 </param>
???????? /// ? <returns> 本節(jié)點的前綴 </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;
????????????}
????????????result? += ?isLast? ? ? " └ " ?:? " ├ " ;
????????????result? += ?hasChild? ? ? " ┬ " ?:? " ─ " ;
???????????? return ?result;
????????}
???????? #region ?綁定下拉菜單
???????? /// ? <summary>
???????? /// ?綁定連動級的下拉菜單
???????? /// ? </summary>
???????? /// ? <param?name="ddlGoodsType"> 傳進(jìn)一個被綁定的DropDownList </param>
???????? /// ? <param?name="removeID"> 被排除綁定的節(jié)點ID </param>
???????? public ? void ?Bind(ListControl?dropDown,? string ?removeID,? string ?parentID)
????????{
????????????
????????????ListItem?listItem? = ? null ;
???????????? string ?currentID? = ?parentID; // 根節(jié)點/父ID
???????????? string ?currentSign? = ? string .Empty; // 當(dāng)前節(jié)點符號;
???????????? string ?parrentSign? = ? string .Empty;? // 父節(jié)點符號;
???????????? bool ?HasChild? = ? true ; // 是否有子
????????????Queue < string > ?parentKeyList? = ? new ?Queue < string > (); // 存?有子節(jié)點的?節(jié)點ID
????????????Queue < string > ?parentSignList? = ? new ?Queue < string > (); // 對應(yīng)節(jié)點ID的前綴符號
???????????? int ?itemIndexOf? = ? 0 ; // 父節(jié)點所在的位置
???????????? while ?(HasChild)
????????????{
???????????????? int ?lastOneCount? = ? 1 ; // 用于計算在同級別中是否最后一個
????????????????Dictionary < string ,? string > ?childList? = ?_DropDownTree.GetChildList(currentID); // ?得到子節(jié)點列表
???????????????? if ?(childList? != ? null )
????????????????{
???????????????????? if ?( ! string .IsNullOrEmpty(removeID)? && ?childList.ContainsKey(removeID))
????????????????????{
????????????????????????childList.Remove(removeID);
????????????????????}
???????????????????? foreach ?(KeyValuePair < string ,? string > ?entry? in ?childList)
????????????????????{
???????????????????????? if ?(_DropDownTree.GetChildList(entry.Key)? != ? null ) // 存在子
????????????????????????{
????????????????????????????currentSign? = ?GetPreFix(lastOneCount? == ?childList.Count,? true ,?parrentSign);
????????????????????????????listItem? = ? new ?ListItem(currentSign? + ?entry.Value,?entry.Key);
????????????????????????????parentKeyList.Enqueue(entry.Key); // 當(dāng)前的節(jié)點ID
????????????????????????????parentSignList.Enqueue(currentSign); // 當(dāng)前的節(jié)點符號
????????????????????????}
???????????????????????? else // 不存在子
????????????????????????{
????????????????????????????currentSign? = ?GetPreFix(lastOneCount? == ?childList.Count,? false ,?parrentSign);
????????????????????????????listItem? = ? new ?ListItem(currentSign? + ?entry.Value,?entry.Key);
????????????????????????}
???????????????????????? if ?(dropDown.Items.Count? != ? 0 )
????????????????????????{
????????????????????????????itemIndexOf? = ? string .IsNullOrEmpty(currentID)? ? ?itemIndexOf? + ? 1 ?:?dropDown.Items.IndexOf(dropDown.Items.FindByValue(currentID))? + ?lastOneCount;
????????????????????????}
????????????????????????dropDown.Items.Insert(itemIndexOf,?listItem); // 添加子節(jié)點
????????????????????????lastOneCount ++ ;
????????????????????}
???????????????????? if ?(parentKeyList.Count? > ? 0 ) // 存在子節(jié)點時
????????????????????{
????????????????????????currentID? = ?parentKeyList.Dequeue();
????????????????????????parrentSign? = ?parentSignList.Dequeue();
????????????????????}
???????????????????? else
????????????????????{
????????????????????????HasChild? = ? false ;
????????????????????}
????????????????}
???????????????? else
????????????????{
???????????????????? break ;
????????????????}
????????????}
????????????_DropDownTree.Dispose();
????????}
???????? #endregion
????}
}
?
二:數(shù)據(jù)庫數(shù)據(jù)準(zhǔn)備
1:為方便示例,這里用了Access數(shù)據(jù)庫,新建一個Product表,并為之添加了幾行數(shù)據(jù),如圖:
?
三:引用CYQ.Data框架來實現(xiàn)
1:項目添加CYQ.Data引用
2:新建ProductTree類,實現(xiàn)IDropDownTree接口
A:為表增加枚舉,如下:
namespace ?Entity{
???? public ? enum ?TableNames
????{
????????Product
????}
???? public ? enum ?Product
????{
????????ID,
????????ParentID,
????????Name
????}
}
?
B:實現(xiàn)接口
using ?Tree;using ?CYQ.Data;
using ?Entity;
using ?CYQ.Data.Table;
using ?System.Collections.Generic;
namespace ?Tree
{
???? /// ? <summary>
???? /// ?作者:路過秋天
???? /// ?博客: http://cyq1162.cnblogs.com
???? /// ?秋色園: http://www.cyqdata.com
???? /// ? </summary>
???? public ? class ?ProductTree?:?IDropDownTree
????{
???????? int ?count? = ? 0 ;
???????? private ?MAction?action;
???????? public ?ProductTree()
????????{
????????????action? = ? new ?MAction(TableNames.Product);
????????}
???????? #region ?IDropDownTree?成員
???????? public ?Dictionary < string ,? string > ?GetChildList( string ?parentID)
????????{
????????????MDataTable?table? = ?action.Select( 0 ,? 0 ,? " ParentID= " ? + ?parentID,? out ?count);
????????????Dictionary < string ,? string > ?dic? = ? null ;
???????????? if ?(count? > ? 0 )
????????????{
????????????????dic? = ? new ?Dictionary < string ,? string > ();
???????????????? foreach ?(MDataRow?row? in ?table.Rows)
????????????????{
????????????????????dic.Add(row.Get < string > (Product.ID),?row.Get < string > (Product.Name));
????????????????}
????????????}
???????????? return ?dic;
????????}
???????? public ?DropDownTree?DropDownTree
????????{
???????????? get
????????????{
???????????????? return ? new ?DropDownTree( this );
????????????}
????????}
???????? #endregion
???????? #region ?IDisposable?成員
???????? public ? void ?Dispose()
????????{
????????????action.Close();
????????}
???????? #endregion
????}
}
項目解決方案如下圖:
?
四:展示應(yīng)用結(jié)果
1:新建測試站點WebDemo項目,并將數(shù)據(jù)庫放到App_Data目錄下,如圖:
2:Web.config配置好數(shù)據(jù)庫鏈接如下:
< appSettings >???? < add? key ="AccessDbNameForWeb" ?value ="App_Data/tree.mdb" />
</ appSettings >
< connectionStrings >
???? < add? name ="Conn" ?connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;?Data?Source={0}" ?providerName ="System.Data.OleDb" />
</ connectionStrings >
?
3:Default.界面調(diào)用代碼
前臺:
放一下拉框: < asp:DropDownList? ID ="ddlProduct" ?runat ="server" ></ asp:DropDownList >?
后臺:
? protected ? void ?Page_Load( object ?sender,?EventArgs?e)?{
????? new ?Tree.ProductTree().DropDownTree.Bind(ddlProduct, null ,? " 0 " );
?}
?
4:最后的輸出結(jié)果,如圖
?
最后提示:
1:提供示例源碼下載:--點擊下載?[最近很少寫文,點擊下載的你別忘了點下推薦哦^-^]]
2:秋色園V2.5將在近期發(fā)布,歡迎提前瀏覽 http://www.cyqdata.com
?
?
PS:傳說點一下推薦會有10個園豆,喜歡麻煩點一下“推薦”,thank you very much!!
?
轉(zhuǎn)載于:https://my.oschina.net/secyaher/blog/274219
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的带线的无限级下拉树列表-完整示例篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: explain
- 下一篇: 分页控件 实战 Post篇