Easyui-ComboTree数据填充,递归。树形节点


http://blog.csdn.net/commandbaby/article/details/51072893

关于递归,和Combotree的数据绑定格式


效果图:

Combotree需要的Json数据格式:http://www.jeasyui.com/demo/main/tree_data1.json

实现方法:

[csharp]  view plain  copy
  1. /* 数据(TitleInfo) 
  2.  *   【 SystemCode:1,    Name:总(副)经理,         父级编码(CodeSystemParentCode):】 
  3.  *   【 SystemCode:2,    Name:总工、助理,          父级编码(CodeSystemParentCode):】 
  4.  *   【 SystemCode:3,    Name:副总工、部门经理(副) 父级编码(CodeSystemParentCode):】 
  5.  *   【 SystemCode:4,    Name:高级项目经理,         父级编码(CodeSystemParentCode):】 
  6.  *   【 SystemCode:412,  Name:技术人员,           父级编码(CodeSystemParentCode):4】 
  7.  *   【 SystemCode:411,  Name:项目经理,           父级编码(CodeSystemParentCode):4】 
  8.  *   【 SystemCode:112,  Name:董事长,            父级编码(CodeSystemParentCode):1】 
  9.  *   【 SystemCode:413,  Name:12312,              父级编码(CodeSystemParentCode):4】 
  10.  */  
  11. public void GetTitleInfo(HttpContext context)  
  12. {  
  13.     string TitileTreestr = "";  
  14.     List<Model.TitleInfo> tiL = BLL.TitleInfo.GetTitleInfos();//所有数据  
  15.     List<Model.TitleInfo> tiLparent = tiL.Where(t => string.IsNullOrEmpty(t.CodeSystemParentCode)).ToList();//父级节点  
  16.   
  17.     //CreateTitileTree(tiL, out TitileTreestr);//普通循环拼接方法  
  18.     CreateTitileTree(tiL, tiLparent, ref TitileTreestr);//递归  
  19.   
  20.     context.Response.Write("[" + TitileTreestr + "]");//[]拼成json格式  
  21.     context.Response.End();  
  22. }  
  23.   
  24. /// <summary>  
  25. /// 职务树(深度只能2层)  
  26. /// </summary>  
  27. /// <param name="tiL">所有职务数据</param>  
  28. /// <returns>返回Json</returns>  
  29. public void CreateTitileTree(List<Model.TitleInfo> tiL, out string ResultTreestr)  
  30. {  
  31.     string TitileTree = "";  
  32.     List<Model.TitleInfo> listParent = tiL.Where(t => string.IsNullOrEmpty(t.CodeSystemParentCode)).ToList(); //父级  
  33.     foreach (var itemp in listParent)//根据父级ID得到子节点  
  34.     {  
  35.         string ChildTree = "";//子节点  
  36.         TitileTree += "{\"id\":\"" + itemp.AriId + "\",\"text\": \"" + itemp.AriName + "\",\"children\":[";  
  37.         List<Model.TitleInfo> listchild = tiL.Where(t => t.CodeSystemParentCode == itemp.SystemCode ).ToList();//该父节点的子ID  
  38.         foreach (var itemc in listchild)  
  39.         {  
  40.             ChildTree += "{\"id\":\"" + itemc.AriId + "\",\"text\": \"" + itemc.AriName + "\",\"children\":[]},";  
  41.         }  
  42.         if (!string.IsNullOrEmpty(ChildTree))  
  43.             ChildTree = ChildTree.Substring(0, ChildTree.Length - 1);  
  44.         TitileTree += ChildTree;  
  45.         TitileTree += "]},";  
  46.     }  
  47.     ResultTreestr = TitileTree.Substring(0, TitileTree.Length - 1);  
  48. }  
  49.   
  50. /// <summary>  
  51. /// 递归获取职务树(无论多深)  
  52. /// </summary>  
  53. /// <param name="tiL">总节点</param>  
  54. /// <param name="tiLparent">父节点</param>  
  55. /// <param name="TitileTree">结果值</param>  
  56. public void CreateTitileTree(List<Model.TitleInfo> tiL, List<Model.TitleInfo> tiLparent, ref string TitileTree)  
  57. {  
  58.     int i = 0;  
  59.     foreach (var itemp in tiLparent)  
  60.     {  
  61.         i++;  
  62.         TitileTree += "{\"id\":\"" + itemp.AriId + "\",\"text\": \"" + itemp.AriName + "\",\"children\":[";  
  63.         List<Model.TitleInfo> child = tiL.Where(t => t.CodeSystemParentCode == itemp.SystemCode).ToList();  
  64.         if (child != null && child.Count() > 0)  
  65.             CreateTitileTree(tiL, child, ref TitileTree);  
  66.         TitileTree += "]},";  
  67.   
  68.         if (i == tiLparent.Count()) //去掉最后一个节点的逗号  
  69.             TitileTree = TitileTree.Substring(0, TitileTree.Length - 1);  
  70.     }  
  71. }  

猜你喜欢

转载自blog.csdn.net/zunguitiancheng/article/details/79045653