西瓜皮

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
using System;
using System.Collections.Generic;
using System.Collections;
using System.Data.Common;
using System.Web.Script.Serialization;
using CYQ.Data;
using CYQ.Data.Table;

public class JsonByEasyUI
{
    /// <summary>
    /// MDataTable转json
    /// </summary>
    /// <param name="table"></param>
    /// <returns></returns>
    public static string JSONForGrid(MDataTable table)
    {
        return table.ToJson();
    }
    /// <summary>
    /// 输出json树型格式
    /// </summary>
    /// <param name="table"></param>
    /// <param name="id"></param>
    /// <param name="pid"></param>
    /// <returns></returns>
    public static string JSONForTree(MDataTable table, string id, string pid)
    {
        string json = string.Empty;
        object obj = JsonByEasyUI.ArrayToTreeData(table, id, pid);
        return new JavaScriptSerializer().Serialize(obj);
    }
    /// <summary>
    /// 获取指定ID的所有上级id,已","分隔
    /// </summary>
    /// <param name="table">表名</param>
    /// <param name="id">id</param>
    /// <param name="pid">pid</param>
    /// <param name="idnum">检索id</param>
    /// <returns></returns>
    public static string GetPath(string table, string id, string pid, int idnum)
    {
        string path = "";
        int Jpid = 0;
        using (MAction action = new MAction(table))
        {
            if (action.Fill(idnum))
            {
                Jpid = action.Get<int>(pid);
            }
        }
        if (Jpid == 0)
        {
            path = "0," + idnum.ToString() + ",";
        }
        else
        {
            if (Jpid != 0)
            {
                path = GetPath(table, id, pid, Jpid);
            }
            path += Jpid.ToString() + "," + idnum.ToString() + ",";
        }
        path = StringHelper.GetArrayStr(StringHelper.GetSubStringList(path, ','), ","); //去重
        return path;
    }
    /// <summary>
    /// 获取树格式对象
    /// </summary>
    /// <param name="list">线性数据</param>
    /// <param name="id">ID的字段名</param>
    /// <param name="pid">PID的字段名</param>
    /// <returns></returns>
    private static object ArrayToTreeData(MDataTable table, string id, string pid)
    {
        var list = MDataTableToHash(table);
        var h = new Hashtable(); //数据索引 
        var r = new List<Hashtable>(); //数据池,要返回的 
        foreach (var item in list)
        {
            if (!item.ContainsKey(id)) continue;
            h[item[id].ToString()] = item;
        }
        foreach (var item in list)
        {
            if (!item.ContainsKey(id)) continue;
            if (!item.ContainsKey(pid) || item[pid] == null || !h.ContainsKey(item[pid].ToString()))
            {
                r.Add(item);
            }
            else
            {
                var pitem = h[item[pid].ToString()] as Hashtable;
                if (!pitem.ContainsKey("children"))
                    pitem["children"] = new List<Hashtable>();
                var children = pitem["children"] as List<Hashtable>;
                children.Add(item);
            }
        }
        return r;
    }
    /// <summary>
    /// 将MDataTable转换为Hashtable列表
    /// </summary>
    /// <param name="reader"></param>
    /// <returns></returns>
    private static List<Hashtable> MDataTableToHash(MDataTable table)
    {
        var list = new List<Hashtable>();
        foreach (MDataRow row in table.Rows)
        {
            var item = new Hashtable();
            for (int i = 0; i < table.FieldCount; i++)
            {
                var name = table.Columns[i].ColumnName; //列名
                var value = row[i].Value; //单元格值
                item[name] = value;
            }
            list.Add(item);
        }
        return list;
    }
}

 

posted on 2013-09-25 15:01  西瓜皮  阅读(618)  评论(0编辑  收藏  举报