合作联系微信: w6668263      合作联系电话:177-9238-7426     

将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();
        }

 

posted on 2015-05-12 18:04  草率的龙果果  阅读(3236)  评论(0编辑  收藏  举报

导航