easyui dataGrid与后台交互传递数据

本文介绍了easyui的dataGrid组件如何与后台进行数据交互。在前台,使用queryParams属性传递json类型参数给后台,并说明了dataGrid默认传递的page和rows参数。同时,配置url指定后台数据接口地址及pageSize设定每页显示的数据量。在后台,通过封装jsonObject,重点讲解了dataGrid需要的total(数据总数)和rows(返回数据)两个关键字段,以及如何封装返回数据类来填充数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;  
    }  
}

 

 

 

 

 

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值