zoukankan      html  css  js  c++  java
  • 适用于zTree 、EasyUI tree、EasyUI treegrid

     #region 
            System.Text.StringBuilder b_appline = new System.Text.StringBuilder();
            System.Text.StringBuilder b_children = new System.Text.StringBuilder();
            /// <summary>
            /// 适用于zTree 、EasyUI tree、EasyUI treegrid
            /// zTree设置id,name,EastyUI 设置 id,text EasyUI treegrid 可自定义
            /// </summary>
            /// <param name="Source">数据源</param>
            /// <param name="_parentidName">父级名称</param>
            /// <param name="_parentidValue">父级顶级节点</param>
            /// <param name="Key">子节点(唯一值)</param>
            /// <returns></returns>
            public string GetTreeJson(DataTable Source, string _parentidName, string _parentidValue, string Key)
            {
                try
                {
                    b_appline.Append(b_children.ToString());
                    b_children.Clear();
                    if (Source.Rows.Count > 0)
                    {
                        b_children.Append("[");
                        DataRow[] row = Source.Select(string.Format("{0}='{1}'", _parentidName, _parentidValue));
                        if (row.Count() > 0)
                        {
                            for (int i = 0; i < row.Count(); i++)
                            {
                                b_children.Append("{");
                                for (int c = 0; c < Source.Columns.Count; c++)
                                {
                                    b_children.AppendFormat(""{0}":"{1}",", Source.Columns[c].ColumnName, row[i].ItemArray[c]);
                                }
                                b_children.Remove(b_children.Length - 1, 1);
                                if (Source.Select(string.Format("{0}='{1}'", _parentidName, row[i][Key].ToString())).Length > 0)
                                {
                                    b_children.Append(","children":");
                                    GetTreeJson(Source, _parentidName,row[i][Key].ToString(), Key);
                                    b_appline.Append(b_children.ToString());
                                    b_children.Clear();
                                }
                                b_appline.Append(b_children.ToString());
                                b_children.Clear();
                                b_children.Append("},");
                            }
                            b_children.Remove(b_children.Length - 1, 1);
                        }
                        b_children.Append("]");
                        b_appline.Append(b_children.ToString());
                        b_children.Clear();
                    }
                    return b_appline.ToString();
                }
                catch (Exception)
                {

                    throw;
                }
            }
            #endregion

  • 相关阅读:
    【初码干货】使用阿里云开放搜索服务快速搭建资源搜索网站
    很认真的聊一聊程序员的自我修养
    【初码干货】使用阿里云邮件推送服务架设自己邮件验证与推送体系
    Supervisor 修改配置文件中的参数值,需要更新服务才能生效
    如何关闭 IntelliJ IDEA 的 Find in Path ?
    Jenkins 服务启动/关闭/重启命令,设置端口
    java.util.ConcurrentModificationException 问题处理
    MySQL 将字符串类型的小数转换为保留位数的小数类型
    Apache Tomcat 文件包含漏洞(CVE-2020-1938)
    Error EElFTPSError: Data channel transfer error (error code is 10054) MobaXterm
  • 原文地址:https://www.cnblogs.com/rancrazy/p/7612011.html
Copyright © 2011-2022 走看看