发布一篇 DataTable 转化为适合 jquery easyui tree,combotree 的json 函数
时间:2011-03-18 来源:$克
显示效果如下:
1 /// <summary>调用方法:
2 /// 递归将DataTable转化为适合jquery easy ui 控件tree ,combotree 的 json
3 /// 该方法最后还要 将结果稍微处理下,将最前面的,"children" 字符去掉.
4 /// </summary>
5 /// <param name="dt">要转化的表</param>
6 /// <param name="pField">表中的父节点字段</param>
7 /// <param name="pValue">表中顶层节点的值,没有 可以输入为0</param>
8 /// <param name="kField">关键字字段名称</param>
9 /// <param name="TextField">要显示的文本 对应的字段</param>
10 /// <returns></returns>
11 public static string TableToEasyUITreeJson(DataTable dt,string pField,string pValue,string kField,string TextField)
12 {
13 StringBuilder sb = new StringBuilder();
14 string filter = String.Format(" {0}='{1}' ", pField, pValue);//获取顶级目录.
15 DataRow[] drs = dt.Select(filter);
16 if (drs.Length < 1)
17 return "";
18 sb.Append(",\"children\":[");
19 foreach (DataRow dr in drs)
20 {
21 string pcv = dr[kField].ToString();
22 sb.Append("{");
23 sb.AppendFormat("\"id\":\"{0}\",",dr[kField].ToString());
24 sb.AppendFormat("\"text\":\"{0}\"",dr[TextField].ToString());
25 sb.Append(TableToEasyUITreeJson(dt, pField, pcv,kField,TextField).TrimEnd(','));
26 sb.Append("},");
27 }
28 if (sb.ToString().EndsWith(","))
29 {
30 sb.Remove(sb.Length - 1, 1);
31 }
32 sb.Append("]");
33 return sb.ToString();
34 }
1 string result = "";html代码
2 BLL.UniversalTree BLLU = new BLL.UniversalTree();
3 DataTable Dt = BLLU.GetAllList().Tables[0];
4 result=TableToEasyUITreeJson(Dt, "parentsid", "0", "sid", "Name");5 if (result.Length > 12)
6 {
7 result = result.Substring(12);
8 }
9 return result;
1 <asp:TextBox id="txtUniverSalSid" runat="server" width="600px"></asp:TextBox>
2
3 $(function() {
4 $("#<%=txtUniverSalSid.ClientID %>").combotree({
5 url: "/ajaxhander/json.ashx?param=getuniversaltree"
6 });
7 });
相关阅读 更多 +