using System; 
 | 
using System.Collections.Generic; 
 | 
using System.Data; 
 | 
using System.Linq; 
 | 
using System.Web; 
 | 
  
 | 
namespace WebAPI.Utility 
 | 
{ 
 | 
    public class TreeUtil 
 | 
    { 
 | 
        public class TreeModel 
 | 
        { 
 | 
            public string id { get; set; } 
 | 
            public string title { get; set; } 
 | 
            public List<TreeModel> children = new List<TreeModel>(); 
 | 
        } 
 | 
  
 | 
        #region 递归函数 
 | 
        /// <summary> 
 | 
        /// 递归函数 
 | 
        /// </summary> 
 | 
        public void digui(DataTable dt, List<TreeModel> tree, int num) 
 | 
        { 
 | 
            for (int m = 0; m < tree.Count; m++) 
 | 
            { 
 | 
                tree[m].children = new List<TreeModel>(); 
 | 
                for (int i = 0; i < dt.Rows.Count; i++)//第一次循环,得到所有根节点的子集 
 | 
                { 
 | 
                    var strLen = dt.Rows[i]["hnumber"].ToString().Split('.'); 
 | 
                    if (strLen.Length == num && dt.Rows[i]["hnumber"].ToString().Contains(tree[m].id + ".")) 
 | 
                    { 
 | 
                        TreeModel tbjson = new TreeModel(); 
 | 
                        tbjson.id = dt.Rows[i]["hitemid"].ToString(); 
 | 
                        tbjson.title = dt.Rows[i]["hname"].ToString(); 
 | 
                        tree[m].children.Add(tbjson); 
 | 
                    } 
 | 
                } 
 | 
                var strLens = tree[m].id.Split('.'); 
 | 
                for (int i = 0; i < tree[m].children.Count; i++) 
 | 
                { 
 | 
                    digui(dt, tree[m].children, strLens.Length + 2);//再次用子集去循环,拿出子集的子集 
 | 
                } 
 | 
            } 
 | 
  
 | 
        } 
 | 
        #endregion 
 | 
  
 | 
        #region 分类 树形图(根据代码展开树状图) 
 | 
        public void getTreeByLevel(DataTable dt, List<TreeModel> tree, int num) 
 | 
        { 
 | 
            for (int m = 0; m < tree.Count; m++) 
 | 
            { 
 | 
                tree[m].children = new List<TreeModel>(); 
 | 
                for (int i = 0; i < dt.Rows.Count; i++)//第一次循环,得到所有根节点的子集 
 | 
                { 
 | 
                    var HLevel = (int)dt.Rows[i]["hlevel"]; 
 | 
                    var HParentID = dt.Rows[i]["hparentid"].ToString(); 
 | 
                    if (HLevel == num && HParentID == tree[m].id) 
 | 
                    { 
 | 
                        TreeModel tbjson = new TreeModel(); 
 | 
                        tbjson.id = dt.Rows[i]["hitemid"].ToString(); 
 | 
                        tbjson.title = dt.Rows[i]["hname"].ToString(); 
 | 
                        tree[m].children.Add(tbjson); 
 | 
                    } 
 | 
                } 
 | 
                for (int i = 0; i < tree[m].children.Count; i++) 
 | 
                { 
 | 
                    getTreeByLevel(dt, tree[m].children, num + 1);//再次用子集去循环,拿出子集的子集 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        #endregion 
 | 
    } 
 | 
} 
 |