中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

數據結構~樹的遍歷(Service層和UI層代碼)

返回目錄

問題是這樣的,Department表是一(yi)(yi)個部(bu)門(men)表,由(you)DeptId,name和Father組成,它(ta)是一(yi)(yi)種樹型的關系,一(yi)(yi)個部(bu)門(men)下可以有(you)多個子部(bu)門(men),同時,它(ta)有(you)一(yi)(yi)個父部(bu)門(men),祖宗部(bu)門(men)沒有(you)父部(bu)門(men)。

以下是測試數據(相當于Data層里取出(chu)數據的方法):

 static List<Department> deptList = new List<Department>
            {
                new Department(1,"根",0),
                new  Department(2,"計算機(ji)",1),
                new  Department(3,"英(ying)語",1),
                new  Department(4,"C語",2),
                new  Department(5,"VB",2),
                new  Department(6,"公共英語",3),
                new  Department(7,"大(da)學英語",3),
                new  Department(8,"公(gong)一級",6),
                new  Department(9,"公二級",6),
                new  Department(10,"公三(san)級",6),
                new  Department(11,"公(gong)四級",6),
            };
存放數據源的變量和為變量賦值的方法
 
#region 加載樹,定義存放樹的變量
        public static void LoadTree()
        {
            DepartmentTree = deptList;
        }
 
        public static List<Department> DepartmentTree = null;
#endregion
 
我們把加載樹放在靜態構造方法里,保存它只被加載一次就可以了,當然如果希望樹自動更新,可以
看文章結尾處
 #region 當前類被第一次訪問或類被第一次建立時間時加載樹
        static Program()
        {
            LoadTree();
        }
 #endregion

以(yi)下是Service層,進行(xing)數(shu)據(ju)讀取的方法:

        #region 樹遞歸Service層
 
        /// <summary>
        /// 完(wan)整樹(shu)
        /// </summary>
        /// <returns></returns>
        public static Department GetTree()
        {
            return GetTree(Department.RootId);
        }
 
        /// <summary>
        /// 找到指定(ding)ID的樹
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public static Department GetTree(int deptId)
        {
            Department root = new Department();
            root = deptList.Where(i => i.DeptId.Equals(deptId)).SingleOrDefault();
            GetSublCategories(root);
            return root;
        }
 
        /// <summary>
        ///找到子孫樹
        /// </summary>
        /// <param name="department">父(fu)對象(xiang)</param>
        static public void GetSublCategories(Department department)
        {
            department.Sons = DepartmentTree.Where(item =>
                item.FatherId.Equals(department.DeptId) && item.DeptId != Department.RootId).ToList();
            department.Sons.ForEach(item =>
            {
                item.Father = department;
                GetSublCategories(item);
            });
 
        }
        #endregion

以下是(shi)UI層,將樹型列表顯示在(zai)屏(ping)幕上的代(dai)碼:

#region 樹遞歸UI層
        public string GetTreeHtml()
        {
            Department department = GetTree();
            StringBuilder html = new StringBuilder();
            html.Append("<ul id='containerul'>");
            this.GetSubCategory(html, department);
            html.Append("</ul>");
            return html.ToString();
        }
 
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="html">HTML字條串組(zu)成樹(shu)型列表</param>
        /// <param name="department">指定對象</param>
        private void GetSubCategory(StringBuilder html, Department department)
        {
            html.Append("<li id='" + department.DeptId + "'>");
            if (department.Sons != null && department.Sons.Count > 0)
            {
                html.Append("<a href='javascript:void(0)'>");
            }
            else
            {

html.Append("<a href='javascript:void(0)' onclick=\"showDCategoryTree('"

+ department.DeptId + "');\">");

            }
            html.Append("<ins>&nbsp;</ins>");
            html.Append(department.DeptId + "-" + department.Name);
            html.Append("</a>");
            if (department.Sons != null && department.Sons.Count > 0)
            {
                html.Append("<ul>");
                foreach (var item in department.Sons)
                {
                    this.GetSubCategory(html, item);
                }
                html.Append("</ul>");
            }
            html.Append("</li>");
        }
        #endregion

如果你(ni)的樹(部門(men))前臺顯示時不會發(fa)生讀寫操作,可以把取數據方法寫在(zai)靜態構造方法里,就像這樣:

 

#region 樹遞歸UI層
        public string GetTreeHtml()
        {
            Department department = GetTree();
            StringBuilder html = new StringBuilder();
            html.Append("<ul id='containerul'>");
            this.GetSubCategory(html, department);
            html.Append("</ul>");
            return html.ToString();
        }
 
 
        /// <summary>
        /// 遞歸所有子樹
        /// </summary>
        /// <param name="html">HTML字條串組成(cheng)樹型列表</param>
        /// <param name="department">指定對象</param>
        private void GetSubCategory(StringBuilder html, Department department)
        {
            html.Append("<li id='" + department.DeptId + "'>");
            if (department.Sons != null && department.Sons.Count > 0)
            {
                html.Append("<a href='javascript:void(0)'>");
            }
            else
            {

html.Append("<a href='javascript:void(0)' onclick=\"showDCategoryTree('"

+ department.DeptId + "');\">");

            }
            html.Append("<ins>&nbsp;</ins>");
            html.Append(department.DeptId + "-" + department.Name);
            html.Append("</a>");
            if (department.Sons != null && department.Sons.Count > 0)
            {
                html.Append("<ul>");
                foreach (var item in department.Sons)
                {
                    this.GetSubCategory(html, item);
                }
                html.Append("</ul>");
            }
            html.Append("</li>");
        }
        #endregion

 

OK,這種方法顯示出(chu)來(lai)的樹(shu)在不(bu)關閉瀏覽器或IIS不(bu)重啟時,樹(shu)如(ru)果被修改(gai)了,它也不(bu)會反映到(dao)樹(shu)上(shang)的,如(ru)果希望改(gai)變(bian)這種情況,讓數(shu)的修改(gai)實時顯示出(chu)來(lai),可以加一個事(shi)件。

#region 每隔1000毫秒去重新加載樹
        static System.Timers.Timer sysTimer = new System.Timers.Timer(1000);
        static void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            LoadTree();
        }
 #endregion

返回目錄

posted @ 2011-09-14 14:55  張占嶺  閱讀(1161)  評論(0)    收藏  舉報