1.前台向后台传递数据
属性queryParams,传递json类型的参数给后台
除了queryParams指定参数外,dataGrid默认传递page rows两个参数给后台。
属性url:指定后台数据接口地址
pageSize:指定每页显示数据量
$('#sourceDG').datagrid({
url:"data.json",
pageSize:10,
queryParams: {
source_type: "",
province_load: "",
city_load: "",
county_load: "",
province_delivery: "",
city_delivery: "",
county_delivery: "",
limited: 10,
offset: (curr-1)*10
},
onBeforeLoad:function(param){
console.log(param);
},
onLoadSuccess:function(data){
console.log(data);
},
onLoadError:function(){
console.log(1);
}
});
2.后台向dataGrid传递数据
封装jsonobject
dataGrid能够接受的key只有下面两个
total:数据总数
rows:返回的数据
封装返回数据类,向其中传参数据总数
totalCount:数据总数
obj:显示数据的list,后面dataGrid中显示的数据就封装在这里,每一个td的name就是list中的key
toGridJson(int totalCount, Object obj)
package com.melon.haul.dto;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Collection;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
public class GridJsonUtil {
public static JSONObject toGridJson(int totalCount, Object obj) {
// 如果数据集对象为null做个特殊处理
if(null == obj) {
JSONObject jsonResult = new JSONObject();
jsonResult.put("total", totalCount);
jsonResult.put("rows", new JSONArray());
return jsonResult;
}
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessor() {
private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
@Override
public Object processArrayValue(Object arg0, JsonConfig arg1) {
return null;
}
@Override
public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
return arg1 == null ? "" : sd.format(arg1);
}
});
JSONArray jsonArray = JSONArray.fromObject(obj,config);
JSONObject jsonResult = new JSONObject();
jsonResult.put("total", totalCount);
jsonResult.put("rows", jsonArray);
return jsonResult;
}
}