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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。