C#学习(8):DataSet构建EasyUI树结构

时间:2021-05-07 10:45:33

1。DataSet或DataTable建EasyUI树Json结构

其实网上已经有很健全的资料了,不过大多都是限制输出某字段的,所有我参考博客园某些博友代码,稍加修改一下:

 1  /// <summary>
 2         /// 根据DataSet构建树Json结构
 3         /// </summary>
 4         /// <param name="dataSet">数据集</param>
 5         /// <param name="treeResult">返回树结构json字符串</param>
 6         /// <param name="rootField">根(父类)字段名称,父类的Id字段名称</param>
 7         /// <param name="rootValue">根(父类)值,父类的值</param>
 8         /// <param name="parRelationField">父类与子类对应关系字段名称,例行的parentId字段名称</param>
 9         /// <returns>Json字符串</returns>
10         public static void GetTreeJsonByTable(DataSet dataSet, StringBuilder treeResult, string rootField, string rootValue, string parRelationField)
11         {
12             DataTable dataTable = dataSet.Tables[0];
13             GetTreeJsonByTable(dataTable, treeResult, rootField, rootValue, parRelationField);
14         }
15 
16         /// <summary>
17         /// 根据Table构建树Json结构
18         /// </summary>
19         /// <param name="dataTable">数据表</param>
20         /// <param name="treeResult">返回树结构json字符串</param>
21         /// <param name="rootField">根(父类)字段名称,父类的Id字段名称</param>
22         /// <param name="rootValue">根(父类)值,父类的值</param>
23         /// <param name="parRelationField">父类与子类对应关系字段名称,例行的parentId字段名称</param>
24         /// <returns>Json字符串</returns>
25         public static void GetTreeJsonByTable(DataTable dataTable, StringBuilder treeResult, string rootField, string rootValue, string parRelationField)
26         {
27             if (dataTable.Rows.Count > 0)
28             {
29                 treeResult.Append("[");
30                 string filer = string.Format("{0}='{1}'", parRelationField, rootValue);
31                 DataRow[] dataRows = dataTable.Select(filer);
32                 if (dataRows.Length > 0)
33                 {
34                     foreach (DataRow dataRow in dataRows)
35                     {
36                         treeResult.Append("{");
37                         foreach (DataColumn dataColumn in dataTable.Columns)
38                         {
39                             treeResult.AppendFormat("\"{0}\":\"{1}\",", dataColumn.ColumnName, dataRow[dataColumn.ColumnName]);
40                         }
41                         treeResult = treeResult.Remove(treeResult.Length - 1, 1);
42                         treeResult.Append(",\"state\":\"open\"");
43 
44                         string rootVal = dataRow[rootField].ToString();
45                         if (dataTable.Select(string.Format("{0}='{1}'", parRelationField, rootVal)).Length > 0)
46                         {
47                             treeResult.Append(",\"children\":");
48                             GetTreeJsonByTable(dataTable, treeResult, rootField, rootVal, parRelationField);
49                         }
50                         treeResult.Append("},");
51                     }
52                     treeResult = treeResult.Remove(treeResult.Length - 1, 1);
53                 }
54                 treeResult.Append("]");
55             }
56         }


调用方法: 

1             StringBuilder monthModelJson = new StringBuilder();
2             EasyUIHelper.GetTreeJsonByTable(ds.Tables[0], monthModelJson, "itemGuid", rootGuid, "parentGuid");