zoukankan      html  css  js  c++  java
  • 将DataTable转成easyui-datagrid tree的JSON格式

            方法1

            /// <summary>
            /// 根据Datatable得到Tree结构的JSON数据(去掉前12个字符,不能包括"")
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="pField"></param>
            /// <param name="pValue"></param>
            /// <param name="kField"></param>
            /// <param name="TextField"></param>
            /// <returns></returns>
            public static string TableToEasyUITreeJson(DataTable dt, string pField, string pValue, string kField, string TextField)
            {
                StringBuilder sb = new StringBuilder();
                string filter = String.Format(" {0}='{1}' ", pField, pValue);//获取顶级目录.
                DataRow[] drs = dt.Select(filter);
                if (drs.Length < 1)
                    return "";
                sb.Append(","children":[");
                foreach (DataRow dr in drs)
                {
                    string pcv = dr[kField].ToString();
                    sb.Append("{");
                    sb.AppendFormat(""ID":"{0}",", dr[kField].ToString().Replace(""", "\""));
                    sb.AppendFormat(""text":"{0}"", dr[TextField].ToString().Replace(""", "\""));
                    sb.Append(TableToEasyUITreeJson(dt, pField, pcv, kField, TextField).TrimEnd(','));
                    sb.Append("},");
                }
                if (sb.ToString().EndsWith(","))
                {
                    sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                return sb.ToString();
            }

    方法2:

    private static string AddNode(string ParentID, int deep, DataSet ds)        

    {            

    string str = "";            

    DataView dvTree = new DataView(ds.Tables[0]);

     dvTree.RowFilter = "[PID]   ='" + ParentID + "'";//过滤出ParentId                      

    int i = 0;            

    int d = deep;            

    foreach (DataRowView drv in dvTree)            

           {

                    if (i == 0)//如果是某一层的开始,需要“[”开始                 

                      {                    

                        if (d == 1)//如果深度为1,即第一层                         

                        str = "[";                    

                       else//否则,为第二层或更深                         

                        str = ","children":[";                

                        }

                    else

                        str = str + ",";                

                        str = str + "{";

                    str = str + ""ID":"" + drv["ID"] + "",";

                    str = str + ""text":"" + drv["Name"] + """;

                    str = str + AddNode(drv["ID"].ToString(), deep + 1, ds);//递归

                    str = str + "}";

                    i = i + 1;

                    if (dvTree.Count == i)//如果某一层到了末尾,需要"]"结束

                          str = str + "]";

                }

                return str;

    }

  • 相关阅读:
    php 7.1 openssl_decrypt() 代替 mcrypt_module_open() 方法
    关于Http_build_query的用法
    git fetch 和git pull 的差别
    PhpStorm 头部注释、类注释和函数注释的设置
    输出信息log4j.properties的作用与使用方法
    字段设置ALV中下拉列表列的实现
    遍历中序C语言实现二叉树的递归遍历与非递归遍历
    搜索中文Solr Analysis And Solr Query Solr分析以及查询
    记忆指向指针常量 常量指针 常量指针常量
    匹配位置KMP算法深入浅出
  • 原文地址:https://www.cnblogs.com/hongyan5682/p/4424183.html
Copyright © 2011-2022 走看看