zoukankan      html  css  js  c++  java
  • C#将datatable生成easyui的绑定tree 的json数据格式

        /// <summary>
        /// 根据DataTable生成Json树结构
        /// </summary>
        /// <param name="tabel">数据源</param>
        /// <param name="idCol">ID列</param>
        /// <param name="txtCol">Text列</param>
        /// <param name="rela">关系字段(字典表中的树结构字段)</param>
        /// <param name="pId">父ID(0)</param>
        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        private void GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId)
        {
            result.Append(sb.ToString());
            sb.Clear();
            if (tabel.Rows.Count > 0)
            {
                sb.Append("[");
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"state\":\"open\"");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            sb.Append(",\"children\":");
                            GetTreeJsonByTable(tabel, idCol, txtCol, rela, row[idCol]);
                            result.Append(sb.ToString());
                            sb.Clear();
                        }
                        result.Append(sb.ToString());
                        sb.Clear();
                        sb.Append("},");
                    }
                    sb = sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                result.Append(sb.ToString());
                sb.Clear();
            }
        }

    GetTreeJsonByTable(datatable, "id", "title", "pid", "0");
    string content = result.ToString();

  • 相关阅读:
    数据结构--线性表和链表的基础知识
    OC基础--字符串
    iOS开发知识梳理博文集
    OC基础--数据类型与表达式
    数据结构概述
    数据结构--栈和队列基础知识
    NodeJS Get/Post 参数获取
    NodeJS + express 添加HTTPS支持
    NodeJS 获取系统时间 并格式化输出
    Node.Js + Express 搭建服务器
  • 原文地址:https://www.cnblogs.com/ybb521/p/2638085.html
Copyright © 2011-2022 走看看