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;
    
        }
    
    }
  • 相关阅读:
    jquery 代码搜集
    Windows Server 2008中安装IIS7.0
    javascript 判断两个日期之间的天数 兼容ie,firefox
    jquery选择器大全
    原始ajax方式调用asp.net后台方法
    JavaScript及C# URI编码详解
    利用JQuery直接调用asp.net后台方法
    C#操作XML小结_转载
    从bnbt tracker源码分析bt客户端与traker的通信
    传说中的神器: shared_ptr/weak_ptr/scoped_ptr
  • 原文地址:https://www.cnblogs.com/thxuaimin/p/3546398.html
Copyright © 2011-2022 走看看