• 通过后台为Ztree拼接parent/children结构的json数据


    <%@ WebHandler Language="C#" Class="Handler" %>
    
    using System;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    public class Handler : IHttpHandler {
        
        string strConn = @"data source=192.168.0.206;initial catalog=XXXX;user id=sa;password=123456";
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/json";
          string sb=  FirstAnsyData2();
            context.Response.Write(sb);
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    
        /// <summary>
        /// 判断当前节点是否还有子节点
        /// </summary>
        /// <param name="ParentId">父节点Id</param>
        /// <returns>bool类型</returns>
        public bool isParentTrue(int ParentId)
        {
            try
            {
                using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn))
                {
                    conn.Open();
                    string sql = "select * from OrginTree where OrgParent =" + ParentId + "";
                    DataTable table = new DataTable();
                    SqlDataAdapter dt = new SqlDataAdapter(sql, conn);
                    dt.Fill(table);
                    return table.Rows.Count >= 1 ? true : false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    
        public string FirstAnsyData2()
        {
            return GetModuleTreeJson("100");
        }
    
        /// <summary>
        /// 生成zTree标准json数据源
        /// </summary>
        /// <param name="parent_id">父节点</param>
        /// <returns></returns>
        public string GetModuleTreeJson(string parent_id)
        {
            StringBuilder jsonTree = new StringBuilder();
            string where = string.Format(" and ORgParent={0}", parent_id);
            DataTable ds = getModuleList(where);
    
            if (ds.Rows.Count > 0)
            {
                jsonTree.Append("[");
                for (int i = 0; i < ds.Rows.Count; i++)
                {
                    DataRow dr = ds.Rows[i];
                    jsonTree.Append("{"id":"").Append(dr["OrgId"]).Append("",");
                    jsonTree.Append(""name":"").Append(dr["OrgName"]).Append("",");
                    if (isParentTrue(int.Parse(dr["OrgId"].ToString())))
                    {
                        jsonTree.Append(""children":").Append(GetModuleTreeJson(dr["OrgId"].ToString()));
                    }
                    else
                    {
                        jsonTree.Remove(jsonTree.Length - 1, 1);
                    }
    
                    jsonTree.Append("},");
                    if (i == (ds.Rows.Count - 1))
                    {
                        jsonTree.Remove(jsonTree.Length - 1, 1);
                    }
                }
                jsonTree.Append("]");
            }
            else
            {
                jsonTree.Append("""");
            }
    
            return jsonTree.ToString();
        }
        public DataTable getModuleList(string whe)
        {
            DataTable ds = new System.Data.DataTable();
            try
            {
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    conn.Open();
                    string sql = "select * from OrginTree where 1=1 " + whe;// where OrgParent is null";
                    SqlDataAdapter dt = new SqlDataAdapter(sql, conn);
                    dt.Fill(ds);
                    conn.Close();
                }
            }
            catch (Exception)
            {
            }
            return ds;
    
        }
    
    }
  • 相关阅读:
    bzoj3688 折线统计
    bzoj3678 wangxz与OJ
    「6月雅礼集训 2017 Day8」route
    「6月雅礼集训 2017 Day8」gcd
    [ SHELL编程 ] 编程常用的ORACLE相关命令
    Oracle数据库备份/导入工具
    Oracle数据文件迁移到裸设备
    Oracle数据文件转移操作
    Oracle重建表空间操作实例
    Linux性能测试分析命令_sar
  • 原文地址:https://www.cnblogs.com/thxuaimin/p/3546398.html
Copyright © 2020-2023  润新知