WD1X.COM - 问答一下,轻松解决,电脑应用解决专家
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win11Linux
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascript服务器
PHP教程CSS教程XML教程

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

更新时间:2021-06-28 15:26 作者:flysun3344点击:

一: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集合。
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容