zoukankan      html  css  js  c++  java
  • 如何从后台拼接json(tree)字符串,给jstree赋值

    public object ListMenu(DataTable _list)
    {
    StringBuilder sb = new StringBuilder();
    DataRow[] rows = _list.Select("PID=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["Name"]);
    sb.AppendFormat(""pid":"{0}",", dr["PID"]);
    string submenu = GetSubMenu(id, _list);
    if (submenu == "")
    {
    sb.AppendFormat(""leaf":"{0}",", "true");
    sb.Append(""children":[");
    }
    else
    {
    sb.AppendFormat(""expanded":"{0}",", "true");
    sb.Append(""children":[");
    sb.Append(submenu);
    }
    sb.Append("]");
    sb.Append("}");
    }
    sb.Append("]");
    _menu = sb.ToString();
    return _menu;
    }


    private string GetSubMenu(string pid, DataTable dt)
    {
    StringBuilder sb = new StringBuilder();
    DataRow[] rows = dt.Select("PID=" + 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["Name"]);
    sb.AppendFormat(""pid":"{0}",", dr["PID"]);
    string submenu = GetSubMenu(id,dt);
    if (submenu == "")
    {
    sb.AppendFormat(""leaf":"{0}",", "true");
    sb.Append(""children":[");
    }
    else
    {
    sb.AppendFormat(""expanded":"{0}",", "true");
    sb.Append(""children":[");
    sb.Append(submenu);
    }
    sb.Append("]");
    sb.Append("}");
    }
    }
    return sb.ToString();
    }

    可根据实际情况修改相应的值:如children可能是nodes

  • 相关阅读:
    撩课-Python-每天5道面试题-第8天
    声明提前、原型、静态方法的一些所得
    梳理ajax
    两数之和、整数反转、回文数
    node 基础API(fs)
    node 基础API(event)
    node 基础API(Buffer)
    node 基础API(path)
    node 调试技巧
    node process(进程) 几个常用属性
  • 原文地址:https://www.cnblogs.com/jinghuimin/p/5279799.html
Copyright © 2011-2022 走看看