1、JSON数据为:

{

    "total":28, "rows": [

    {

      "itemNo": "1",

      "itemName": "12",

      "spec": "",

      "pattern": "",

      "itemCategory": {

        "id": "A01",

        "name": "中药"

      },

      "itemUnit": {

        "id": "B01",

        "name": "公斤"

      },

      "uploadFileName": null

    },

  ],



}

2、其相应的java类为:

public class PageModel<T> {



//结果集

private List<T>
rows;

private int total;



//getter and setter methods



}

注意:因为EasyUI中的datagrid组件处理该数据时,默认都是使用rows属性和total属性的,所以为了json数据符合要求,java类中的list属性名比方为rows,list个数的属性名必须为total。

for(var i=0;i<rows.length;i++){

...

}

3、List中的class例如以下:

public class Item {

private String itemNo;



private String itemName;



private String spec;



private String pattern;



private ItemCategory itemCategory;



private ItemUnit itemUnit;



private String uploadFileName;

//getter and setter methods

}

public class ItemCategory {

 

private String id; 



private String name;

//getter and setter methods

}

public class ItemUnit {

 

private String id; 



private String name;

//getter and setter methods

}

4、SpringMVC中处理showJSON的Controller:

@ResponseBody

protected Object showJson(HttpServletRequest request, HttpServletResponse response)

throws Exception {



PageModel pageModel = get_page_model_from_database();

return pageModel;

}

直接返回PageModel 对象,@ResponseBody注解会自己主动调用JSON库将其转换成JSON格式的数据,即本文开头的数据;

5、datagrid中载入数据

//载入数据  

$('#Ajax').datagrid({

fit:true,

fitColumns:true,

width: 'auto',  

height:300,               

striped: true,  

singleSelect : true,  

url:'item.do',  

//POST parameters: command=showJson

queryParams:{

command: 'showJson'

},  

loadMsg:'数据载入中请稍后……',  

pagination: true,  

rownumbers: true,     

columns:[[  

{field:'itemNo',title: '物料代码',align: 'center',width: 100,  

formatter:function(value,row,index){  

//alert("itemNo: "+value+" "+row.itemNo+" "+index);  

return "<a href='item.do?

command=showDetail&itemNo="+value+"'>"+value+"</a>";

  }  

},  

{field:'itemName',title: '物料名称',align: 'center',width: 100},  

{field:'spec',title: '
物料规格',align: 'center',width: 100},  

{field:'pattern',title: '物料型号',align: 'center',width: 100},

{field:'itemCategory',title: '类别',align: 'center',width: 100,

formatter:function(value,row,index){

var ret="";



if(value != null) {

ret = value.name;

//alert("itemCategory.id="+value.id+" name="+value.name);

}

return ret;

}



},

{field:'itemUnit',title: '计量单位',align: 'center',width: 100,

formatter:function(value,row,index){

var ret="";



if(value != null) {

ret = value.name;

//alert("itemCategory.id="+value.id+" name="+value.name);

}

return ret;

}



},                                  

    

                  

]]  

});

处理简单数据,比方 "itemNo": "1"时直接使用{field:'itemNo',title: '物料代码',align: 'center',width: 100}就可以载入数据。

但处理复杂数据,比方

"itemCategory": {

        "id": "A01",

        "name": "中药"

      },

则须要使用formatter属性来载入。代码例如以下:

{field:'itemCategory',title: '类别',align: 'center',width: 100,

formatter:function(value,row,index){

var ret="";



if(value != null) {

ret = value.name;

//alert("itemCategory.id="+value.id+" name="+value.name);

}

return ret;

}



},

关于datagrid formatter属性的描写叙述參考:http://www.jeasyui.com/documentation/index.php#

The cell formatter function, take three parameters:

value: the field value.表示当前field相应的值。能够是一个简单对象。如一个字符串,也能够是一个复杂对象。相应一个java类

row: the row record data.表示当前行相应的整个对象,相当于json数据中的rows[i]对象(i为当前行的index),能够使用row.itemNo訪问JSON数据中的rows[i].itemNo对象。使用row.itemCategory.id訪问JSON数据中的rows[i].itemCategory.id对象。

index: the row index.行号。第一行的行号为0

Code example:

$('#dg').datagrid({
columns:[[
{field:'userId',title:'User', width:80,
formatter: function(value,row,index){
if (row.user){
return row.user.name;
} else {
return value;
}
}
}
]]
});

Jquery easyUI datagrid载入复杂JSON数据方法的更多相关文章

  1. jQuery EasyUI datagrid实现本地分页的方法

    http://www.codeweblog.com/jquery-easyui-datagrid%e5%ae%9e%e7%8e%b0%e6%9c%ac%e5%9c%b0%e5%88%86%e9%a1% ...

  2. jquery easyui datagrid 远程加载数据----javascript法

    jquery easyui有三种办法生成datagrid(数据网格),本篇专门讨论javascript借助jquey easy ui实现的方式 html部分 <main role="m ...

  3. jquery easyui datagrid 远程加载数据----把主键渲染为值遇到的问题及解决方案

    起因:数据库中一些字段存的是代表具体值的数字,需要渲染为具体值 monggodb中的字典 mysql中存放的值为:expertin代表教练擅长的搏击技能 jquery easyui中的相关代码如下:用 ...

  4. Easyui datagrid 绑定本地Json数据

    var jsonstr = '{"total":1,"rows":[{"id":"M000005","name ...

  5. (转)扩展jquery easyui datagrid 之动态绑定列和数据

    本文转载自:http://blog.csdn.net/littlewolf766/article/details/7336550 easyui datagrid 不支持动态加载列,上次使用的方法是自己 ...

  6. jQuery EasyUI DataGrid Checkbox 数据设定与取值

    纯粹做个记录,以免日后忘记该怎么设定. 这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数 ...

  7. 用easyui从servlet传递json数据到前端页面的两种方法

    用easyui从servlet传递json数据到前端页面的两种方法 两种方法获取的数据在servlet层传递的方法相同,下面为Servlet中代码,以查询表中所有信息为例. //重写doGet方法 p ...

  8. jquery easyui datagrid 获取Checked选择行(勾选行)数据

    原文:jquery easyui datagrid 获取Checked选择行(勾选行)数据 getSelected:取得第一个选中行数据,如果没有选中行,则返回 null,否则返回记录. getSel ...

  9. 扩充 jQuery EasyUI Datagrid 数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)

    客户需求: jQuery EasyUI Datagrid 用户列表鼠标悬停/离开数据行时显示人员头像(onMouseOver/onMouseOut) 如图所示,Datagrid 鼠标悬停/离开数据行时 ...

随机推荐

  1. Linux文件目录的一点小结

    转载:http://blog.chinaunix.net/uid-29171357-id-3889735.html 1. 相关指令:      chgrp:改变文件所属用户组 点击(此处)折叠或打开 ...

  2. Appium+python自动化16-appium1.6在mac上环境搭建启动ios模拟器上Safari浏览器

    前言 在mac上搭建appium踩了不少坑,先是版本低了,启动后无限重启模拟器.后来全部升级最新版本,就稳稳的了. 环境准备: 1.OS版本号10.12 2.xcode版本号8.3.2 3.appiu ...

  3. vc预处理

    VC 编译命令开关 vc可以可以通过Settings -->Project-->C/C++-->Customize来设置这个编译开关 /C:在预处理输出中保留注释语句 /c:只编译, ...

  4. [转]Hamcrest使用方法实例

    assertThat方法需要使用hamcrest的匹配方法: 示例 assertThat( n, allOf( greaterThan(1), lessThan(15) ) ); assertThat ...

  5. 1644 免费馅饼 题解(c++)

    1644 免费馅饼(巴蜀oj上的编号) 题面:          SERKOI最新推出了一种叫做"免费馅饼"的游戏.         游戏在一个舞台上进行.舞台的宽度为W格,天幕的 ...

  6. Element 'beans' cannot have character [children]

    在编写spring的applicationContext.xml文件时,出现了: Element 'beans' cannot have character [children], because t ...

  7. 刚子扯谈:网站运营在左 技术在右 真TM扯

    朋友的书 鄙人的书 2013年8月5日,雨未下,天猴焖 开片语:今天的扯谈内容是我转载我Java学习交流群里面一个哥们,当然我推荐他加入了朋友的网络分析师这个群,我认为那样对他有更大帮助.现在他是XX ...

  8. mysql 下 计算 两点 经纬度 之间的距离

    公式如下,单位米: 第一点经纬度:lng1 lat1 第二点经纬度:lng2 lat2 round(6378.138*2*asin(sqrt(pow(sin( (lat1*pi()/180-lat2* ...

  9. Mac版小黑屋提示无法确认开发者身份的解决办法

    Mac版小黑屋提示无法确认开发者身份的解决办法 学习了:https://jingyan.baidu.com/article/37bce2be703fa21003f3a259.html 需要按住cont ...

  10. STL - C++ 11的Lambda表达式(上)

    Lambda始自C++ 11,是一种在表达式或语句内指定函数行为的定义式. 你可以定义函数行为作为对象,以inline实参的形式传给算法作为predicate(判断式). eg: std:transf ...