效果图:
Combotree需要的Json数据格式:http://www.jeasyui.com/demo/main/tree_data1.json
实现方法:
[csharp] view plain copy /* 数据(TitleInfo) * 【 SystemCode:1, Name:总(副)经理, 父级编码(CodeSystemParentCode):】 * 【 SystemCode:2, Name:总工、助理, 父级编码(CodeSystemParentCode):】 * 【 SystemCode:3, Name:副总工、部门经理(副) 父级编码(CodeSystemParentCode):】 * 【 SystemCode:4, Name:高级项目经理, 父级编码(CodeSystemParentCode):】 * 【 SystemCode:412, Name:技术人员, 父级编码(CodeSystemParentCode):4】 * 【 SystemCode:411, Name:项目经理, 父级编码(CodeSystemParentCode):4】 * 【 SystemCode:112, Name:董事长, 父级编码(CodeSystemParentCode):1】 * 【 SystemCode:413, Name:12312, 父级编码(CodeSystemParentCode):4】 */ public void GetTitleInfo(HttpContext context) { string TitileTreestr = ""; List<Model.TitleInfo> tiL = BLL.TitleInfo.GetTitleInfos();//所有数据 List<Model.TitleInfo> tiLparent = tiL.Where(t => string.IsNullOrEmpty(t.CodeSystemParentCode)).ToList();//父级节点 //CreateTitileTree(tiL, out TitileTreestr);//普通循环拼接方法 CreateTitileTree(tiL, tiLparent, ref TitileTreestr);//递归 context.Response.Write("[" + TitileTreestr + "]");//[]拼成json格式 context.Response.End(); } /// <summary> /// 职务树(深度只能2层) /// </summary> /// <param name="tiL">所有职务数据</param> /// <returns>返回Json</returns> public void CreateTitileTree(List<Model.TitleInfo> tiL, out string ResultTreestr) { string TitileTree = ""; List<Model.TitleInfo> listParent = tiL.Where(t => string.IsNullOrEmpty(t.CodeSystemParentCode)).ToList(); //父级 foreach (var itemp in listParent)//根据父级ID得到子节点 { string ChildTree = "";//子节点 TitileTree += "{\"id\":\"" + itemp.AriId + "\",\"text\": \"" + itemp.AriName + "\",\"children\":["; List<Model.TitleInfo> listchild = tiL.Where(t => t.CodeSystemParentCode == itemp.SystemCode ).ToList();//该父节点的子ID foreach (var itemc in listchild) { ChildTree += "{\"id\":\"" + itemc.AriId + "\",\"text\": \"" + itemc.AriName + "\",\"children\":[]},"; } if (!string.IsNullOrEmpty(ChildTree)) ChildTree = ChildTree.Substring(0, ChildTree.Length - 1); TitileTree += ChildTree; TitileTree += "]},"; } ResultTreestr = TitileTree.Substring(0, TitileTree.Length - 1); } /// <summary> /// 递归获取职务树(无论多深) /// </summary> /// <param name="tiL">总节点</param> /// <param name="tiLparent">父节点</param> /// <param name="TitileTree">结果值</param> public void CreateTitileTree(List<Model.TitleInfo> tiL, List<Model.TitleInfo> tiLparent, ref string TitileTree) { int i = 0; foreach (var itemp in tiLparent) { i++; TitileTree += "{\"id\":\"" + itemp.AriId + "\",\"text\": \"" + itemp.AriName + "\",\"children\":["; List<Model.TitleInfo> child = tiL.Where(t => t.CodeSystemParentCode == itemp.SystemCode).ToList(); if (child != null && child.Count() > 0) CreateTitileTree(tiL, child, ref TitileTree); TitileTree += "]},"; if (i == tiLparent.Count()) //去掉最后一个节点的逗号 TitileTree = TitileTree.Substring(0, TitileTree.Length - 1); } }