zoukankan      html  css  js  c++  java
  • 将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();
    }

  • 相关阅读:
    东驴西磨
    深入理解计算机系统 第二章信息的表示和处理(笔记小结)
    Amdahl 阿姆达尔定律(系统优化)
    想你的夜——葱油饼
    TypeScript and AWS Lambda practicing road map
    "Silicon Valley-like" companies think of engineers as value generators
    original intentions
    Canopy is hiring Postgres SQL Engineer
    在多线程中 feign 调用,服务提供方拿不到 request 的错误
    CF1542D
  • 原文地址:https://www.cnblogs.com/yangpeng-jingjing/p/6032733.html
Copyright © 2011-2022 走看看