将DataTable生成树json

 protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                ListMenu();
            }
        }

        protected string _menu = string.Empty;
        public void ListMenu()
        {
            StringBuilder sb = new StringBuilder();
            DataTable _list = new DataTable();
            string ConctionStr = "Data Source=127.0.0.1;Initial Catalog=AccordMenu;Integrated Security=False;User ID=sa;Password=sa";
            using (SqlConnection conec = new SqlConnection(ConctionStr))
            {
                conec.Open();
                string SqlText = "SELECT * FROM sys_menu";
                SqlCommand Comand = new SqlCommand(SqlText, conec);
                SqlDataAdapter Adaper = new SqlDataAdapter(Comand);
                Adaper.Fill(_list);
                Adaper.Dispose();
                Comand.Dispose();
                conec.Close();
            }
            DataRow[] rows = _list.Select("ParentID=0");
            sb.Append("[");
            bool isFist = false;
            foreach (DataRow dr in rows)
            {
                if (isFist)
                    sb.Append(",");
                isFist = true;
                string id = dr["ID"].ToString();
                sb.Append("{");
                sb.AppendFormat("\"id\":\"{0}\",", dr["ID"]);
                sb.AppendFormat("\"text\":\"{0}\",", dr["MenuName"]);
                sb.AppendFormat("\"pid\":\"{0}\",", dr["ParentID"]);
                sb.AppendFormat("\"iconCls\":\"icon_{0}\",", dr["Menu_ICON"]);
                sb.AppendFormat("\"ICON_ID\":\"{0}\",", dr["Menu_ICON"]);
                sb.AppendFormat("\"url\":\"{0}\",", dr["Menu_URL"]);
                sb.AppendFormat("\"ICON_URL\":\"{0}\"", dr["ICON_URL"]);
                sb.Append(",children:[");
                sb.Append(GetSubMenu(id, _list));
                sb.Append("]");
                sb.Append("}");
            }
            sb.Append("]");
            _menu = sb.ToString();

        }

        /// <summary>
        /// 递归调用生成无限级别
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        private string GetSubMenu(string pid, DataTable dt)
        {
            StringBuilder sb = new StringBuilder();
            DataRow[] rows = dt.Select("ParentID=" + pid);
            if (rows.Length > 0)
            {
                bool isFist = false;
                foreach (DataRow dr in rows)
                {
                    if (isFist)
                        sb.Append(",");
                    isFist = true;
                    string id = dr["ID"].ToString();
                    sb.Append("{");
                    sb.AppendFormat("\"id\":\"{0}\",", dr["ID"]);
                    sb.AppendFormat("\"text\":\"{0}\",", dr["MenuName"]);
                    sb.AppendFormat("\"pid\":\"{0}\",", dr["ParentID"]);
                    sb.AppendFormat("\"iconCls\":\"icon_{0}\",", dr["Menu_ICON"]);
                    sb.AppendFormat("\"ICON_ID\":\"{0}\",", dr["Menu_ICON"]);
                    sb.AppendFormat("\"url\":\"{0}\",", dr["Menu_URL"]);
                    sb.AppendFormat("\"ICON_URL\":\"{0}\"", dr["ICON_URL"]);
                    sb.Append(",children:[");
                    sb.Append(GetSubMenu(id, dt));
                    sb.Append("]");
                    sb.Append("}");
                }
            }
            return sb.ToString();
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值