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

122 篇文章 0 订阅
5 篇文章 0 订阅


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. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
easyui-combotree是一种基于EasyUI框架的组件,用于在网页中创建一个带有树状结构的下拉菜单。它可以用于选择树状结构中的某个节点。通过设置相关参数,可以实现默认选中多项、选择节点以及获取节点信息等功能。你可以使用HTML代码来创建一个easyui-combotree,如下所示: ``` <input id="ProjectTree" class="easyui-combotree" style="width: 300px;" /> ``` 在后端代码中,你可以通过获取区域节点信息的方式来提供easyui-combotree所需的数据。例如,在后端代码中可以使用以下方法来获取三级区域菜单的数据: ```csharp public ActionResult GetThreeRegionTree(string parentRegionCode = "") { List<TreeRegionOne> treeOneList = new List<TreeRegionOne>(); var treeListModel = Sys_RegionServer.GetRegionChildList(parentRegionCode); foreach (var oneTreeItem in treeListModel) { TreeRegionOne treeOne = new TreeRegionOne(); treeOne.id = Convert.ToInt32(oneTreeItem.Value);//下拉框value值 treeOne.text = oneTreeItem.Text;//显示文本 treeOne.state = "open";//显示的是文件夹样式还是文档样式,也就是是否最后一级 //是否有子节点 var childList = Sys_RegionServer.GetRegionChildList(oneTreeItem.Value); if (childList != null && childList.Count() > 0) { treeOne.state = "closed"; } treeOneList.Add(treeOne); } return Json(treeOneList); } ``` 上述代码中,通过遍历区域节点信息,构建一个TreeRegionOne对象列表,包含了节点的id、显示文本和是否有子节点等信息。最后,通过Json方法返回这个列表作为easyui-combotree数据源。这样,easyui-combotree就会显示出相应的树结构,并且可以根据你的需求进行选择和操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [easyui combotree树形下拉框)使用收获总结](https://blog.csdn.net/weixin_44311648/article/details/118654862)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [easyui学习记录:combotree的使用](https://blog.csdn.net/weixin_30319153/article/details/97892869)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值