• 如何从后台拼接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

  • 相关阅读:
    数据结构一
    MVC5.0(一)
    异步多线程(六)lock锁
    异步多线程(五)多线程异常处理
    异步多线程(四)Task
    paypal payflow设置视频教程
    Java栈Stack知识点
    Java知识树梳理
    js定时器
    jdk环境变量配置改变不生效的问题
  • 原文地址:https://www.cnblogs.com/jinghuimin/p/5279799.html
Copyright © 2020-2023  润新知