zoukankan      html  css  js  c++  java
  • 通过后台为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;
    
        }
    
    }
  • 相关阅读:
    基础总结篇之一:Activity生命周期
    putExtra方法
    fork函数
    linux中fork()函数详解(原创!!实例讲解) (转载)
    系统调用wait、waitpid和exec函数
    版本控制工具VSS使用介绍
    学习进度条六(第七周)
    阶段冲刺4
    阶段冲刺3
    阶段冲刺2
  • 原文地址:https://www.cnblogs.com/thxuaimin/p/3546398.html
Copyright © 2011-2022 走看看