码迷,mamicode.com
首页 > 其他好文 > 详细

EasyUI ComboTree数据绑定树形分类显示

时间:2014-05-05 23:06:02      阅读:433      评论:0      收藏:0      [点我收藏+]

标签:style   class   tar   ext   color   width   

承接上篇博文【LINQ获取树形分类的层数】。在上文中,笔者只分享了层数,在这里我把完整的实现贴出来,欢迎批评指正。

先附上效果图:

bubuko.com,布布扣

 

bubuko.com,布布扣

首先是Tree公共类

 
  1. public class Tree  
  2.    {  
  3.        public int ModuleID { getset; }  
  4.  
  5.        public int ParentID { getset; }  
  6.  
  7.        public int ModulePath { getset; }  
  8.  
  9.        public string ModuleName { getset; }  
  10.    }  

 

接下来就是TreeHelper了,这个类是获取json数据的核心他将数据库中的数据组装成了EasyUI可以接受的json格式。

 
  1. public class DepartmentTreeHelper  
  2.   {  
  3.       private static ZhaopinModels db = new ZhaopinModels();  
  4.  
  5.       #region 获取父类集合  
  6.  
  7.       public static IList<Tree> returnParentTree()  
  8.       {  
  9.           IList<Tree> trees = new List<Tree>();  
  10.           List<TUserDepartment> departments = db.TUserDepartments.Where(d => d.department_pid == 0).ToList();  
  11.  
  12.           foreach (var userDepartment in departments)  
  13.           {  
  14.               Tree tree = new Tree();  
  15.               tree.ModuleID = userDepartment.id;  
  16.               tree.ParentID = (int) userDepartment.department_pid;  
  17.               tree.ModulePath = GetLayerById(tree.ModuleID);  
  18.               tree.ModuleName = userDepartment.department_name;  
  19.               trees.Add(tree);  
  20.           }  
  21.           return trees;  
  22.       }  
  23.  
  24.       #endregion  
  25.  
  26.       #region 获取分类的层数  
  27.  
  28.       /// <summary>  
  29.       /// 获取分类的层数  
  30.       /// </summary>  
  31.       /// <param name="id">根据id获取树的层数</param>  
  32.       /// <returns>树的层数</returns>  
  33.       private static int GetLayerById(int id)  
  34.       {  
  35.           int layer = 1;  
  36.           for (  
  37.               int subId =  
  38.                   (int)  
  39.                       db.TUserDepartments.Where(d => d.id == id)  
  40.                           .Select(d => d.department_pid)  
  41.                           .ToList()  
  42.                           .FirstOrDefault();  
  43.               subId != 0;)  
  44.           {  
  45.               subId =  
  46.                   (int)  
  47.                       db.TUserDepartments.Where(d => d.id == subId)  
  48.                           .Select(d => d.department_pid)  
  49.                           .ToList()  
  50.                           .FirstOrDefault();  
  51.               layer++;  
  52.           }  
  53.           return layer;  
  54.       }  
  55.  
  56.       #endregion  
  57.  
  58.       #region 判断分类是否有子类  
  59.  
  60.       /// <summary>  
  61.       /// 判断分类是否有子类  
  62.       /// </summary>  
  63.       /// <param name="id"></param>  
  64.       /// <returns></returns>  
  65.       public static bool IsHaveChild(int id)  
  66.       {  
  67.           bool flag = false;  
  68.           var did = db.TUserDepartments.Where(d => d.department_pid == id).Select(d => d.id).ToList();  
  69.           if (did.Count > 0)  
  70.           {  
  71.               flag = true;  
  72.           }  
  73.           return flag;  
  74.       }  
  75.  
  76.       #endregion  
  77.  
  78.       #region 根据id获取子类  
  79.  
  80.       /// <summary>  
  81.       /// 根据id获取子类  
  82.       /// </summary>  
  83.       /// <param name="id"></param>  
  84.       /// <returns></returns>  
  85.       public static IList<Tree> GetChild(int id)  
  86.       {  
  87.           IList<Tree> t = new List<Tree>();  
  88.           List<TUserDepartment> departments = db.TUserDepartments.Where(d => d.department_pid == id).ToList();  
  89.           foreach (var d in departments)  
  90.           {  
  91.               Tree tParent = new Tree();  
  92.               tParent.ModuleID = d.id;  
  93.               tParent.ParentID = (int) d.department_pid;  
  94.               tParent.ModulePath = DepartmentTreeHelper.GetLayerById(tParent.ModuleID);  
  95.               tParent.ModuleName = d.department_name;  
  96.  
  97.               t.Add(tParent);  
  98.           }  
  99.           return t;  
  100.       }  
  101.  
  102.       #endregion  
  103.  
  104.       #region 获取json  
  105.  
  106.       /// <summary>  
  107.       /// 获取json  
  108.       /// </summary>  
  109.       /// <returns></returns>  
  110.       public static string GetJson()  
  111.       {  
  112.           string json = "[";  
  113.           IList<Tree> t = DepartmentTreeHelper.returnParentTree();  
  114.           foreach (Tree model in t)  
  115.           {  
  116.               if (model != t[t.Count - 1])  
  117.               {  
  118.                   json += DepartmentTreeHelper.GetJsonByModel(model) + ",";  
  119.               }  
  120.               else 
  121.               {  
  122.                   json += DepartmentTreeHelper.GetJsonByModel(model);  
  123.               }  
  124.           }  
  125.           json += "]";  
  126.           json = json.Replace("‘""\"");  
  127.           return json;  
  128.       }  
  129.  
  130.       #endregion  
  131.  
  132.       #region 根据模型生成json  
  133.  
  134.       /// <summary>  
  135.       /// 根据模型生成json  
  136.       /// </summary>  
  137.       /// <param name="t"></param>  
  138.       /// <returns></returns>  
  139.       public static string GetJsonByModel(Tree t)  
  140.       {  
  141.           string json = "";  
  142.           bool flag = DepartmentTreeHelper.IsHaveChild(t.ModuleID);  
  143.  
  144.           json = "{" 
  145.                  + "‘id‘:‘" + t.ModuleID + "‘," 
  146.                  + "‘text‘:‘" + t.ModuleName + "‘," 
  147.                  + "‘iconCls‘:‘ok‘," 
  148.                  + "‘children‘:";  
  149.           if (!flag)  
  150.           {  
  151.               json += "null}";  
  152.           }  
  153.           else 
  154.           {  
  155.               json += "[";  
  156.               IList<Tree> list = DepartmentTreeHelper.GetChild(t.ModuleID);  
  157.               foreach (Tree tree in list)  
  158.               {  
  159.                   if (tree != list[list.Count - 1])  
  160.                   {  
  161.                       json += GetJsonByModel(tree) + ",";  
  162.                   }  
  163.                   else 
  164.                   {  
  165.                       json += GetJsonByModel(tree);  
  166.                   }  
  167.               }  
  168.               json += "]}";  
  169.           }  
  170.           return json;  
  171.       }  
  172.  
  173.       #endregion  
  174.   } 
 

EasyUI ComboTree数据绑定树形分类显示,布布扣,bubuko.com

EasyUI ComboTree数据绑定树形分类显示

标签:style   class   tar   ext   color   width   

原文地址:http://www.cnblogs.com/tangyouwei/p/3704737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!