通过DataTable 返回Json格式,用于绑定Easyui tree
2013-02-05 15:40
316 查看
/// <summary> /// 根据DataTable生成Json树结构 調用GetTreeJsonByTable(datatable, "MENUID", "MENUNAME", "PARENTID", "0"); /// </summary> /// <param name="tabel">数据源</param> /// <param name="idCol">ID列</param> /// <param name="txtCol">Text列</param> /// <param name="rela">关系字段</param> /// <param name="pId">父ID</param> ///<returns>easyui tree json格式</returns> StringBuilder treeResult = new StringBuilder(); StringBuilder treesb = new StringBuilder(); public string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId) { string treeJson = string.Empty; treeResult.Append(treesb.ToString()); treesb.Clear(); if (tabel.Rows.Count > 0) { treesb.Append("["); string filer = string.Empty; if (pId.ToString() == "") { filer = string.Format("{0} is null", rela); } else { filer = string.Format("{0}='{1}'", rela, pId); } DataRow[] rows = tabel.Select(filer); if (rows.Length > 0) { foreach (DataRow row in rows) { treesb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"state\":\"open\""); if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0) { treesb.Append(",\"children\":"); GetTreeJsonByTable(tabel, idCol, txtCol, rela, row[idCol]); treeResult.Append(treesb.ToString()); treesb.Clear(); } treeResult.Append(treesb.ToString()); treesb.Clear(); treesb.Append("},"); } treesb = treesb.Remove(treesb.Length - 1, 1); } treesb.Append("]"); treeResult.Append(treesb.ToString()); treeJson = treeResult.ToString(); treesb.Clear(); } return treeJson; }
调用方法:
MenuTreeHelper treeHelper = new MenuTreeHelper();
return treeHelper.GetTreeJsonByTable(datatable, "ID", "TEXT", "PARENTNO", "0");
相关文章推荐
- django通过ajax发起请求返回JSON格式数据的方法
- 如何通过Java的Web服务器把DataSet或DataTable数据通过json格式传回给C#的客户端
- C#将datatable生成easyui的绑定tree 的json数据格式
- C#将datatable生成easyui的绑定tree 的json数据格式
- 在本示例中,我们将向您展示如何将对象转换成json格式并通过spring mvc框架返回给用户。
- 统计:后台通过查询数据表(该表存放统计的sql语句)返回规定格式的json数据
- PHP读取目录下所有文件,并通过ajax返回json格式的数据
- JDBC- exercise-2 通过htttp 访问 数据库,返回一个JSON格式的相应信息
- 把DataTable 转换成Json格式,适用于EasyUI 绑定DataGrid
- spring mvc 中通过responsebody返回json格式到jsp页面乱码解决方法
- C#将datatable生成easyui的绑定tree 的json数据格式
- 夺命雷公狗jquery---54通过ajax的底层实现返回json格式的数据
- C#将datatable生成easyui的绑定tree 的json数据格式
- spring mvc 返回通过配置ResponseBody返回json格式的数据
- 通过gradle生成SpringMVC实现RESTful返回JSON格式数据
- C#将datatable生成easyui的绑定tree 的json数据格式
- spring接口通过配置支持返回多种格式(xml,json,html,excel)
- 通过json格式返回服务器对象
- 把DataTable 转换成Json格式,适用于EasyUI 绑定DataGrid
- django通过ajax发起请求返回JSON格式数据的方法