• 用友U8存货分类通过DataTable生成EasyUI Tree JSON


    <%@ WebHandler Language="C#" Class="InventoryClass" %>
    
    using System;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using Newtonsoft.Json;
    
    public class InventoryClass : IHttpHandler
    {
        DataTable dt = new DataTable();
        string json = string.Empty;
        public void ProcessRequest(HttpContext context)
        {
            //选取所有成品类别
            using (SqlConnection conn = new SqlConnection(DB.ConnectionStrings.U8))
            using (SqlCommand cmd = new SqlCommand("SELECT cInvCCode as id ,cInvCCode + ' ' + cInvCName as text,iInvCGrade ,bInvCEnd FROM InventoryClass WHERE cInvCCode LIKE 'A%' order by cInvCCode", conn))
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                conn.Open();
                da.Fill(dt);
                //第一个成品类别是A开头,级别为1
                getJson("A%", 1);
                context.Response.Write(json.Substring(11, json.Length - 13));
            }
        }
    
        void getJson(string filter, int iInvCGrade)
        {
            //筛选下一级的分类
            DataView dv = new DataView(dt, @"id like '" + filter + "' and iInvCGrade = " + iInvCGrade.ToString(), "", DataViewRowState.CurrentRows);
            if (dv.Count > 0)
            {
                //添加children
                if (!string.IsNullOrEmpty(json))
                {
                    json = json.TrimEnd(',').TrimEnd('}') + ","children":[";
                }
                else
                {
                    json += ""children":[";
                }
                //循环每一行
                for (int i = 0; i < dv.Count; i++)
                {
                    //JsonConvert用DataTable比较方便
                    DataTable dt2 = dt.Clone();
                    dt2.ImportRow(dv[i].Row);
                    string s = JsonConvert.SerializeObject(dt2);
                    json += s.Remove(0, 1).TrimEnd(']') + ",";      //移除前后的[],并加上,
                    //递归获取下级分类
                    getJson(dv[i]["id"].ToString() + "%", iInvCGrade + 1);
                    dt2.Clear();
                }
                //本层循环完成后去掉后面的,并加上]},
                json = json.TrimEnd(',') + "]},";
            }
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    
    }



    生成的JSON可以用于JSON树、下拉列表、网格等

    生成树效果:


  • 相关阅读:
    Robotium源码分析之Instrumentation进阶
    路飞学城Python-Day115
    路飞学城Python-Day114
    路飞学城Python-Day113
    【逻辑回归的特征筛选方法】
    路飞学城Python-Day108
    路飞学城Python-Day107
    【算法学习】神经网络
    路飞学城Python-Day100
    路飞学城Python-Day101
  • 原文地址:https://www.cnblogs.com/apollokk/p/6713838.html
Copyright © 2020-2023  润新知