jquery easyui中datagrid数据传递和返回的格式

本文介绍了jQuery EasyUI的datagrid组件在数据传递至后台和返回前台时的格式。在数据传递给后台时,包括开发者自定义的数据和datagrid封装的数据。在数据返回时,后台需返回一个包含total和rows属性的JSON对象,以供datagrid正确显示。同时,文章提到了Integer类型空值在转化为JSON时可能出现的问题及其解决方案。

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

一:datagrid数据传递给后台

    datagrid数据传递给后台分为,1:开发者自己需要传递至后台的数据  2:datagrid封装的传递的数据。下面分别介绍:

    1. 开发者自己需要传递至后台的数据

      定义好的datagrid。

      <table id="datagrid"></table>

$('#datagrid').datagrid('options').url ='******/*****.action';

$('#datagrid').datagrid('options').queryParams = {
		'ProductVo.itemId' : itemId,
		'ProductVo.productId' : productId,
		'ProductVo.listPrice' : listPrice,
		'ProductVo.status' : status
	};

$('#datagrid').datagrid('reload');

如果项目中使用的struts的Action,直接在action中定义好ProductVo的属性,并设置getter和setter方法,就可以接受到这些参数了。

  2:datagrid封装的传递的数据

    datagrid使用了分页的话,其框架自身会向后台传递page、rows这个两个属性值。分别表示当前页和当前页显示的记录行数。可以在action中定义好这两个属性,并同样设置getter和setter方法,就可以接受到这些参数了。
    
private int page; // 第几页
private int rows; // 行数

public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

二:datagrid数据的返回(后台传给前台)

后台传给前台datagrid数据的格式如下:


如果上面这些属性的Vo类为ProductVo。后台查询数据库返回List<ProductVo> obj;

则通过通过下面代码将数据传给前台datagrid:

int totalcount = obj.size();

JSONObject resultObj = toGridJson(totalcount ,obj);具体的toGridJson方法如下:

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

        if(!Collection.class.isAssignableFrom(obj.getClass())) {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("total", totalCount);
            jsonResult.put("rows", new JSONArray());
            return jsonResult;
        }

        JSONArray jsonArray = JSONArray.fromObject(obj);
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("total", totalCount);
        jsonResult.put("rows", jsonArray);
        return jsonResult;
    }

在使用JSONAray的转化对象时,存在Integer、Long等类型的参数如果为空会有默认赋值问题,导致datagrid显示的值就有问题,可使用下面方法解决:

/**
     * 生成datagrid的需要格式 可以杜绝Long、Integer类型的默认赋值问题
     * 
     * @param totalCount
     * @param obj
     * @return
     */
    public static JSONObject toJsonString(int totalCount, Object obj) {
        if(null == obj) {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("total", totalCount);
            jsonResult.put("rows", new JSONArray());
            return jsonResult;
        }
        if(!Collection.class.isAssignableFrom(obj.getClass())) {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("total", totalCount);
            jsonResult.put("rows", new JSONArray());
            return jsonResult;
        }
        String json = toJsonString(obj);
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("total", totalCount);
        jsonResult.put("rows", json);
        return jsonResult;
    }

public static String toJsonString(Object obj) {
        String json = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            json = mapper.writeValueAsString(obj);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

上面的ObjectMapper来自:


你可能注意到了代码中的total、rows,datagrid接收数据只认识这两个属性。

然后将封装好的resultObj写给页面就可以了:

public void ajaxPrintPage(Object resultObj) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = null;
		try {
			try {
				writer = response.getWriter();
				if (null == obj) {
					writer.print("");
				} else {
					writer.print(obj.toString());
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} finally {
			if (writer != null) {
				writer.flush();
				writer.close();
			}
		}
	}


注意:

datagrid前台传给后台和后台传给前台都有一个rows属性。但是这两个属性的名称虽然相同,但是值得类型确不一样。前台传给后台的rows为一个数字值,标识当前页的记录数。后台传给前台的rows为一个数组对象,数组中的内容即为数据Vo集合。





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值