首页 > 代码库 > C#生成easyui tree-grid中的json字符串
C#生成easyui tree-grid中的json字符串
最近项目用到EasyUI的treegrid控件实现树形Table,实现效果如下:
treegrid需要的json字符串样式如下
[{ "id":1, "name":"C", "size":"", "date":"02/19/2010", "children":[{ "id":2, "name":"Program Files", "size":"120 MB", "date":"03/20/2010", "children":[{ "id":21, "name":"Java", "size":"", "date":"01/13/2010", "state":"closed", "children":[{ "id":211, "name":"java.exe", "size":"142 KB", "date":"01/13/2010" },{ "id":212, "name":"jawt.dll", "size":"5 KB", "date":"01/13/2010" }] }]}]
对于树形结构数据库中数据表的设计一般为ID、Name、ParentID,C#代码设计如下:
1 public class TreeUtil 2 { 3 public static string Json(List<Hashtable> dataList) 4 { 5 // 节点列表(映射表,用于临时存储节点对象) 6 Hashtable nodeList = new Hashtable(); 7 // 将结果集存入映射表(后面将借助映射表构造多叉树) 8 for (int i = 0; i < dataList.Count; i++) 9 { 10 Hashtable dataRecord = dataList[i]; 11 Node node = new Node(); 12 node.menuid = (String)dataRecord["menuid"]; 13 node.menuname = (String)dataRecord["text"]; 14 node.parentId = (String)dataRecord["parentId"]; 15 node.icon = (String)dataRecord["icon"]; 16 node.url = (String)dataRecord["url"]; 17 nodeList.Add(node.menuid, node); 18 } 19 // 根节点 20 Node root = null; 21 //构造无序的多叉树 22 System.Collections.IDictionaryEnumerator enumerator = nodeList.GetEnumerator(); 23 while (enumerator.MoveNext()) 24 { 25 Node node = enumerator.Value as Node; 26 if (string.IsNullOrEmpty(node.parentId)) 27 { 28 root = node; 29 } 30 else 31 { 32 ((Node)nodeList[node.parentId]).addChild(node); 33 } 34 } 35 return root.ToString(); 36 } 37 /// <summary> 38 /// 节点类 39 /// </summary> 40 class Node 41 { 42 //ID 43 public String menuid; 44 45 //Name 46 public String menuname; 47 48 //父项ID 49 public String parentId; 50 51 public String icon; 52 public String url; 53 54 //子项列表 55 public List<Node> menus = new List<Node>(); 56 57 // 添加孩子节点 58 public void addChild(Node node) 59 { 60 menus.Add(node); 61 } 62 63 // 先序遍历,拼接JSON字符串 64 public override String ToString() 65 { 66 String result = "{\"" + "menuid\": \"" + menuid + "\"" + ",\"menuname\" : \"" + menuname + "\","; 67 if (string.IsNullOrEmpty(icon)) 68 { 69 result += "\"icon\":\"icon-sys\","; 70 } 71 else 72 { 73 result += "\"icon\":\"" + icon + "\","; 74 } 75 if (!string.IsNullOrEmpty(url)) 76 { 77 result += "\"url\":\"" + url + "\","; 78 } 79 if (menus.Count != 0) 80 { 81 result += "\"menus\" : ["; 82 for (int i = 0; i < menus.Count; i++) 83 { 84 result += ((Node)menus[i]).ToString() + ","; 85 } 86 result = result.Substring(0, result.Length - 1); 87 result += "]"; 88 } 89 else 90 { 91 result = result.Substring(0, result.Length - 1); 92 } 93 return result + "}"; 94 } 95 96 // 兄弟节点横向排序 97 public void sortChildren() 98 { 99 if (menus.Count != 0)100 {101 // 对本层节点进行排序(可根据不同的排序属性,传入不同的比较器,这里 传入ID比较器)102 menus.Sort(new NodeIDComparator());103 // 对每个节点的下一层节点进行排序 104 for (int i = 0; i < menus.Count; i++)105 {106 ((Node)menus[i]).sortChildren();107 }108 }109 }110 111 }112 113 /// <summary>114 /// 节点比较器(自定义排序规则)115 /// </summary>116 class NodeIDComparator : IComparer<Node>117 {118 // 按照节点编号比较119 120 int IComparer<Node>.Compare(Node o1, Node o2)121 {122 int j1 = Convert.ToInt32(((Node)o1).menuid);123 int j2 = Convert.ToInt32(((Node)o2).menuid);124 return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));125 }126 }127 128 }129 130 131 /// <summary>132 /// 虚拟获取数据库中的元数据133 /// </summary>134 class VirtualDataGenerator135 {136 // 构造无序的结果集列表,实际应用中,该数据应该从数据库中查询获得;137 public static List<Hashtable> getVirtualResult()138 {139 List<Hashtable> dataList = new List<Hashtable>();140 141 Hashtable dataRecord1 = new Hashtable();142 dataRecord1.Add("menuid", "112000");143 dataRecord1.Add("text", "功能菜单1");144 dataRecord1.Add("parentId", "110000");145 146 Hashtable dataRecord2 = new Hashtable();147 dataRecord2.Add("menuid", "112200");148 dataRecord2.Add("text", "子菜单1");149 dataRecord2.Add("parentId", "112000");150 151 Hashtable dataRecord3 = new Hashtable();152 dataRecord3.Add("menuid", "112100");153 dataRecord3.Add("text", "子菜单2");154 dataRecord3.Add("parentId", "112000");155 156 Hashtable dataRecord4 = new Hashtable();157 dataRecord4.Add("menuid", "113000");158 dataRecord4.Add("text", "测试1");159 dataRecord4.Add("parentId", "110000");160 161 Hashtable dataRecord5 = new Hashtable();162 dataRecord5.Add("menuid", "100000");163 dataRecord5.Add("text", "测试2");164 dataRecord5.Add("parentId", "");165 166 Hashtable dataRecord6 = new Hashtable();167 dataRecord6.Add("menuid", "110000");168 dataRecord6.Add("text", "测试3");169 dataRecord6.Add("parentId", "100000");170 171 Hashtable dataRecord7 = new Hashtable();172 dataRecord7.Add("menuid", "111000");173 dataRecord7.Add("text", "测试4");174 dataRecord7.Add("parentId", "110000");175 176 dataList.Add(dataRecord1);177 dataList.Add(dataRecord2);178 dataList.Add(dataRecord3);179 dataList.Add(dataRecord4);180 dataList.Add(dataRecord5);181 dataList.Add(dataRecord6);182 dataList.Add(dataRecord7);183 184 return dataList;185 }186 }
附SQLServer查询树形结构的SQL语句
with AgentTree(Code,Name,ParentCode) as ( select DL_Agent.Code,DL_Agent.Name,DL_Agent.ParentCode FROM DL_Agent where DL_Agent.Code=‘1‘ union all select DL_Agent.Code,DL_Agent.Name,DL_Agent.ParentCode from DL_Agent,AgentTree where DL_Agent.ParentCode = AgentTree.Code) select * from AgentTree;
参考:java实现此类json字符串,特此感谢原作者
http://www.iteye.com/topic/1122125
C#生成easyui tree-grid中的json字符串
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。