一个递归处理json tree树节点帮助类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Hosting;
using System.Security.Permissions;
using System.Text;
using ClientControl;

/// <summary>
///ExtTree 的摘要说明
/// </summary>
public class ExtTree
{
    private string _XMLpath;
    private static ExtTree extTree = null;
    public static ExtTree Current
    {
        get
        {
            if (extTree == null)
                extTree = new ExtTree();

            return extTree;
        }
    }

    DataTable dtTree = new DataTable();

    private string logidID = string.Empty;
    private string sessionID = string.Empty;

 private ExtTree()
 {
        //InitXMLpath();
        //GetAllNodes();
 }

    private void InitXMLpath()
    {
        string xmlpath = "~/app_data/tree.xml";
        _XMLpath = HostingEnvironment.MapPath(xmlpath);
        FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Write, _XMLpath);
        permission.Demand();
    }

    private DataTable GetAllNodes()
    {
        //ds.ReadXml(_XMLpath);
        dtTree = MainClientControl.Instance().GetTree(this.logidID, this.sessionID);
        return dtTree;
    }

    private DataTable GetAllNodes(string parentid)
    {
        //DataTable dt = GetAllNodes();
        DataTable dt = dtTree;
        DataTable _dt = dt.Clone();

        //DataRow[] drs = dt.Select("parentid = '" + parentid .ToString()+ "'");
        //if (drs.Length > 0)
        //{
        //    foreach (DataRow dr in drs)
        //    {
        //        _dt.ImportRow(dr);
        //    }
        //}
        foreach (DataRow dr in dt.Rows)
        {
            if (dr["parentid"].ToString() == parentid.ToString())
                _dt.Rows.Add(dr.ItemArray);
        }
        return _dt;
    }

 

    public string CreateExtTreeJSON(string logidID,string sessionID)
    {
        this.logidID = logidID;
        this.sessionID = sessionID;
        GetAllNodes();
        StringBuilder sb = new StringBuilder();
        CreateExtTreeNode(sb);
        string s= sb.ToString();
        return s.Replace("}{","},{");
    }
    /*
     * Ext Tree JSON 数据部分属性说明:
     * text: 要显示的节点文件
     * id:这个就不用解释了
     * href:链接地址
     * hrefTarget:链接目标框架名称
     * children:子节点 格式:[{节点1},{节点2}...]
     * leaf:当前节点是否为叶子节点。如果为false 则此节点有子节点。
     *                                  否则为true,此节点在无子节点
     * */
    private void CreateExtTreeNode(StringBuilder sb)
    {
        DataTable dt = GetAllNodes("0");
        if (dt.Rows.Count > 0)
        {
            sb.Append("[");
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append("{");
                sb.Append("text:'" + dr["title"].ToString() + "',");
                if (this.logidID == "9999")
                {
                    sb.Append("checked:false,");
                }
                //sb.Append("id:'node" + dr["id"].ToString() + "',");
                sb.Append("id:'" + dr["id"].ToString() + "',");
                sb.Append("href:'" + dr["url"].ToString() + "',");
                sb.Append("hrefTarget:'main'");
                AddChildrenNode(GetAllNodes(dr["id"].ToString()),sb);
                sb.Append("}");
            }
        }

        sb.Append("]");
    }

    private void AddChildrenNode(DataTable dt,StringBuilder sb)
    {
        if (dt.Rows.Count > 0)
        {
            sb.Append(",leaf:false,children:[");
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append("{");
                sb.Append("text:'" + dr["title"].ToString() + "',");
                if (this.logidID == "9999")
                {
                    sb.Append("checked:false,");
                }
                //sb.Append("id:'node" + dr["id"].ToString() + "',");
                sb.Append("id:'" + dr["id"].ToString() + "',");
                sb.Append("href:'" + dr["url"].ToString() + "',");
                sb.Append("hrefTarget:'main'");
                AddChildrenNode(GetAllNodes(dr["id"].ToString()), sb);
                sb.Append("}");
            }

            sb.Append("]");
        }
        else
            sb.Append(",leaf:true");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值