EasyUI datagrid动态加载json数据
最近做一个项目,要求是两张张表可能查找出10多种不同的结果集。
如果想只用一个表格就把全部的结果不同的显示出来那么就肯定不同使用固定的字段名字,要通过动态加载后台返回来的数据把它显示出来就必须动态加载column
具体看下面
1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5 <link href="../Script/jquery-easyui-1.4.4/themes/default/easyui.css" rel="stylesheet" />
6 <link href="../Script/jquery-easyui-1.4.4/themes/icon.css" rel="stylesheet" />
7 <script src="../Script/jquery-1.8.2.js"></script>
8 <script src="../Script/jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
9
10 <script type="text/javascript">
11 $(function () {
12 //动态加载标题和数据
13 $.ajax({
14 url: "../ashx/GetDataList.ashx",
15 type: "post",
16 dataType: "json",
17 success: function (data) {
18 var msg = $.parseJSON(data);
19 $("#dg").datagrid({
20 columns: [data.title] //动态取标题
21 });
22 $("#dg").datagrid("loadData", data.rows); //动态取数据
23 }
24 });
25 });
26 </script>
27 <title></title>
28 </head>
29 <body>
30 <table id="dg" data-options="
31 rownumbers:true,
32 singleSelect:true,
33 autoRowHeight:false,
34 pagination:true,
35 resizeHandle:'right'">
36 <thead>
37 <tr></tr>
38 </thead>
39 </table>
40 </body>
41 </html>
1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5 <link href="../Script/jquery-easyui-1.4.4/themes/default/easyui.css" rel="stylesheet" />
6 <link href="../Script/jquery-easyui-1.4.4/themes/icon.css" rel="stylesheet" />
7 <script src="../Script/jquery-1.8.2.js"></script>
8 <script src="../Script/jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
9
10 <script type="text/javascript">
11 $(function () {
12 //动态加载标题和数据
13 $.ajax({
14 url: "../ashx/GetDataList.ashx",
15 type: "post",
16 dataType: "json",
17 success: function (data) {
18 var msg = $.parseJSON(data);
19 $("#dg").datagrid({
20 columns: [data.title] //动态取标题
21 });
22 $("#dg").datagrid("loadData", data.rows); //动态取数据
23 }
24 });
25 });
26 </script>
27 <title></title>
28 </head>
29 <body>
30 <table id="dg" data-options="
31 rownumbers:true,
32 singleSelect:true,
33 autoRowHeight:false,
34 pagination:true,
35 resizeHandle:'right'">
36 <thead>
37 <tr></tr>
38 </thead>
39 </table>
40 </body>
41 </html>
后台代码我只写出关键的部分,在数据库查询出一个DataTable传入,最后直接返回就是json对象的
2 /// 把DataTable数据转换为Json格式
3 /// </summary>
4 /// <param name="dt">传入DataTable数据</param>
5 /// <returns></returns>
6 public string DataTableToJson(DataTable dt, int pageTotal)
7 {
8 StringBuilder jsonBuilder = new StringBuilder();
9 jsonBuilder.Append("{\"total\"");
10 jsonBuilder.Append(":");
11 jsonBuilder.Append(pageTotal);
12 jsonBuilder.Append(",\"rows");
13 jsonBuilder.Append("\":[");
14 for (int i = 0; i < dt.Rows.Count; i++)
15 {
16 jsonBuilder.Append("{");
17 for (int j = 0; j < dt.Columns.Count; j++)
18 {
19 jsonBuilder.Append("\"");
20 jsonBuilder.Append(dt.Columns[j].ColumnName);
21 jsonBuilder.Append("\":\"");
22 jsonBuilder.Append(dt.Rows[i][j].ToString());
23 jsonBuilder.Append("\",");
24 }
25 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
26 jsonBuilder.Append("},");
27 }
28 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
29 jsonBuilder.Append("],");
30 jsonBuilder.Append("\"title");
31 jsonBuilder.Append(dt.TableName);
32 jsonBuilder.Append("\":[");
33 //这是循环获取列名称
34 for (int n = 0; n < dt.Columns.Count; n++)
35 {
36 jsonBuilder.Append("{");
37 jsonBuilder.Append("\"field");
38 jsonBuilder.Append("\":\"");
39 jsonBuilder.Append(dt.Columns[n].ColumnName);
40 jsonBuilder.Append("\",");
41 jsonBuilder.Append("\"title");
42 jsonBuilder.Append("\":\"");
43 jsonBuilder.Append(dt.Columns[n].ColumnName);
44 jsonBuilder.Append("\"");
45 jsonBuilder.Append("},");
46 }
47 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
48 jsonBuilder.Append("},");
49
50 jsonBuilder.Remove(jsonBuilder.Length - 2, 2);
51 jsonBuilder.Append("]");
52 jsonBuilder.Append("}");
53 return jsonBuilder.ToString();
54 }
1 /// <summary>
2 /// 把DataTable数据转换为Json格式
3 /// </summary>
4 /// <param name="dt">传入DataTable数据</param>
5 /// <returns></returns>
6 public string DataTableToJson(DataTable dt, int pageTotal)
7 {
8 StringBuilder jsonBuilder = new StringBuilder();
9 jsonBuilder.Append("{\"total\"");
10 jsonBuilder.Append(":");
11 jsonBuilder.Append(pageTotal);
12 jsonBuilder.Append(",\"rows");
13 jsonBuilder.Append("\":[");
14 for (int i = 0; i < dt.Rows.Count; i++)
15 {
16 jsonBuilder.Append("{");
17 for (int j = 0; j < dt.Columns.Count; j++)
18 {
19 jsonBuilder.Append("\"");
20 jsonBuilder.Append(dt.Columns[j].ColumnName);
21 jsonBuilder.Append("\":\"");
22 jsonBuilder.Append(dt.Rows[i][j].ToString());
23 jsonBuilder.Append("\",");
24 }
25 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
26 jsonBuilder.Append("},");
27 }
28 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
29 jsonBuilder.Append("],");
30 jsonBuilder.Append("\"title");
31 jsonBuilder.Append(dt.TableName);
32 jsonBuilder.Append("\":[");
33 //这是循环获取列名称
34 for (int n = 0; n < dt.Columns.Count; n++)
35 {
36 jsonBuilder.Append("{");
37 jsonBuilder.Append("\"field");
38 jsonBuilder.Append("\":\"");
39 jsonBuilder.Append(dt.Columns[n].ColumnName);
40 jsonBuilder.Append("\",");
41 jsonBuilder.Append("\"title");
42 jsonBuilder.Append("\":\"");
43 jsonBuilder.Append(dt.Columns[n].ColumnName);
44 jsonBuilder.Append("\"");
45 jsonBuilder.Append("},");
46 }
47 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
48 jsonBuilder.Append("},");
49
50 jsonBuilder.Remove(jsonBuilder.Length - 2, 2);
51 jsonBuilder.Append("]");
52 jsonBuilder.Append("}");
53 return jsonBuilder.ToString();
54 }
如果你显示不出来,那么就参考下面的格式,一定要符合这种格式,一定要符合这种格式,一定要符合这种格式,重要事情说三遍
2 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
3 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
4 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
5 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
6 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
7 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
8 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
9 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"}
10 ],
11 "title":[
12 {
13 "field":"id",
14 "title":"公司自编码"
15 },
16 {
17 "field":"name",
18 "title":"公司名称"
19 },
20 {
21 "field":"coding",
22 "title":"编码"
23 },
24 {
25 "field":"abbreviation",
26 "title":"公司简称"
27 },
28 {
29 "field":"industryRegistrationId",
30 "title":"工商注册号"
31 },
32 {
33 "field":"corporation",
34 "title":"公司法人"
35 }
36 ]
37 }
1 {"total":8,"rows":[
2 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
3 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
4 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
5 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
6 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
7 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
8 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"},
9 {"id":"101","name":"正义无限公司","coding":"01","abbreviation":"正义","industryRegistrationId":"1123456","corporation":"张三"}
10 ],
11 "title":[
12 {
13 "field":"id",
14 "title":"公司自编码"
15 },
16 {
17 "field":"name",
18 "title":"公司名称"
19 },
20 {
21 "field":"coding",
22 "title":"编码"
23 },
24 {
25 "field":"abbreviation",
26 "title":"公司简称"
27 },
28 {
29 "field":"industryRegistrationId",
30 "title":"工商注册号"
31 },
32 {
33 "field":"corporation",
34 "title":"公司法人"
35 }
36 ]
37 }
我是是参考这个大神做的,我也是弄很久不行就是因为json格式不对。
http://my.oschina.net/missGu/blog/393069?fromerr=ltaovswk
EasyUI datagrid动态加载json数据的更多相关文章
- jquery easyui datagrid 动态 加载列
实现方式: 首先根据输入的sql语句获得相关的列名称返回给前台,然后在datagrid中动态加载列,接着根据查询条件(包括sql语句)获取相关的记录返回给前台用于填充datagrid.从而实现类似or ...
- easyui datagrid 动态加载数据 渲染问题,表格错位问题
$('#dg').datagrid({ url:'datagrid_data.json', columns:[[ {field:'code',title:'Code',width:100}, {fie ...
- EasyUI datagrid 动态加载表头和数据
首先返回到页面的需要是JSON数据: 第一步: 遍历表头,插入到array中 for (var i = 0; i < jsonObj.title.length; i++) { //把返回的数据封 ...
- 【第一篇】说说MVC+EF easyui dataGrid 动态加载分页表格
首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...
- 转 -- MVC+EF easyui dataGrid 动态加载分页表格
首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...
- easyui combobox 动态加载数组数据
怕自己忘了,记录下来以后用方便 html部分 <input id="rzcode" name="businesItemId" style="wi ...
- MVC4中EasyUI Tree异步加载JSON数据生成树
1,首先构造tree接受的格式化数据结构MODEL /// <summary> /// 定义EasyUI树的相关数据,方便控制器生成Json数据进行传递 /// </summar ...
- geotrellis使用(二十三)动态加载时间序列数据
目录 前言 实现方法 总结 一.前言 今天要介绍的绝对是华丽的干货.比如我们从互联网上下载到了一系列(每天或者月平均等)的MODIS数据,我们怎么能够对比同一区域不同时间的数据情况,采用 ...
- OpenLayers学习笔记(十)— 动态加载JSON数据模拟航迹线
在openlayers 3 上,加载本地json数据,动态绘制航迹线,以飞机当前位置为地图中心,此例子是模拟DEMO 本文链接:动态加载JSON数据模拟航迹线 作者:狐狸家的鱼 GitHub:八至 前 ...
随机推荐
- rebtree学习
http://www.cnblogs.com/skywang12345/p/3245399.html http://www.cnblogs.com/skywang12345/p/3624177.htm ...
- 从入门到自闭之python三大器--装饰器
开放封闭原则:在不修改源代码及调用方式,对功能进行额外添加就是开放封闭原则 开放:对代码的扩展进行开发 封闭:修改源代码 装饰(额外功能) 器:工具(函数) 普通版: # print(time.tim ...
- Ubuntu下软件打开时状态图标与原始图标不重合问题解决
问题描述 如下图书所示,pycharm打开时,运行的pycharm图标与原始的在收藏夹中的图标不重合.而其他软件不会这样,运行软件的图标与原始图标会重合, 解决办法 把鼠标悬浮在打开的pycharm上 ...
- HTML5的几大新特性
为了更好地处理今天的互联网应用,HTML5添加了很多新元素及功能,比如: 图形的绘制,多媒体内容,更好的页面结构,更好的形式 处理,和几个api拖放元素,定位,包括网页 应用程序缓存,存储,网络工作者 ...
- python 中if __name__ = '__main__' 的作用
python 中if __name__ = '__main__' 的作用 前言 首先我们要知道在python里面万物皆对象,模块也是对象,并且所有的模块都有一个内置属性 __name__. 一个模块的 ...
- 实现MD5算法
using System; using System.Text; using System.Security.Cryptography; namespace Common { /// <summ ...
- multi gpu inference with tfserving or keras
在tfserving上,目测只能在每个gpu上起docker https://github.com/tensorflow/serving/issues/311#issuecomment-4801 ...
- 【学习】024 springCloud
单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N个子 ...
- Thiago2(TPO AI.ROSTO):集成式AI换脸软件(Autodesk Flame)
如标题一样,Thiago2 是一款集成式AI换脸软件(TPO AI.ROSTO),需要与Autodesk Flame结合使用,从demo来看完成度还是很高的,算是一种完全GUI版的DeepFaceLa ...
- SQLServer Transaction Isolation Level
基本用法 -- Syntax for SQL Server and Azure SQL Database SET TRANSACTION ISOLATION LEVEL { READ UNCOMMIT ...