C# DropDownList裏的樹形創建

        private void GetListForDropDownList()
        {
            try
            {
                List<Fun> TopFunList = Fun.GetTopFunList();//頂級目錄集合

                HashArray haUpIdWithDownList = new HashArray();//所有功能集合
                List<Fun> childFunList = Fun.GetFunListByParentFunId(" where FunId<>0 order by Sort");
                foreach (Fun xfun in childFunList)
                {
                    haUpIdWithDownList.Add(xfun.ParentFun_Id, xfun);
                }

                StringBuilder ahtml = new StringBuilder();//創建樹型option
                foreach (Fun ofun in TopFunList)
                {

                    ahtml.AppendFormat("<option value='{0}'>{1}({2})</option>", ofun.FunId, ofun.FuncName, ofun.Sort);

                    CreateTree2(ahtml, haUpIdWithDownList, ofun.FunId,1,"",false);

                }


                Context.Response.Write(SJsonMessage.SJsonMessageString(SJsonMessageType.Sucess, "加載成功!", ahtml.ToString()));
            }
            catch (Exception ex)
            {

                ArrayList exceptionlist = new ArrayList();
                exceptionlist.Add("添加失敗!");
                exceptionlist.Add("錯誤原因:" + ex.Message);
                Context.Response.Write(SJsonMessage.SJsonMessageString(SJsonMessageType.Error,
                    CheckMessage.ProcessMessage(exceptionlist, CheckMessageType.顯示文字li), ""));
            }
        }

        private void CreateTree2(StringBuilder sb, HashArray aUpsWithDownFun, int parentId, int margin,string op,bool topislast)
        {
            ArrayList downFuns = aUpsWithDownFun.Load(parentId);//獲取子對像
            downFuns.Sort(new Sortx());//排序
            margin++;
            string sp = "";
            string tmp = op.Replace("  ", "┃");
            for (int i = 1; i < margin - 1 - tmp.Length; i++)
            {
                   sp+="┃";
            }
           

            string bb = "┣";
                

            int c = 1;

            foreach (Fun ofun in downFuns)
            {
               
                if (topislast)//如果上級是最後一個
                {
                    sp = sp.Replace("┃", "  ");
                }

                bool la = false;
                if (c == downFuns.Count)//如果是本級的最後一個FUN對像
                {
                    la = true;
                    bb = "┗";
                }
                StringBuilder ahtml = new StringBuilder();
                ahtml.AppendFormat("<option value='{0}'>{1}({2})</option>", 
                    ofun.FunId, op.Replace(" ", " ") + sp.Replace(" ", " ") + bb + ofun.FuncName, ofun.Sort);
                CreateTree2(ahtml, aUpsWithDownFun, ofun.FunId, margin, op + sp, la);
                sb.Append(ahtml.ToString());
                c++;

            }
        }

        public class Sortx : IComparer
        {

            public int Compare(object x, object y)
            {
                return ((Fun)x).Sort.CompareTo(((Fun)y).Sort);
            }
        }

最終效果


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章