封装一个jqGrid的数据源类

这2个月刚换单位,比较赶进度.很久不更新blog,今天抽空看了一下jqGrid,因为现在工作环境又迁到java了,但是是1.4的呵呵,所以封装了一个jqGrid的数据源,方便使用

ps: 需要json-simple [url=http://code.google.com/p/json-simple]点我[/url]


package com.javaeye.rikugun.jqGrid;

import java.io.IOException;
import java.io.Writer;
import java.util.LinkedList;
import java.util.List;

import org.json.simple.JSONAware;
import org.json.simple.JSONObject;
import org.json.simple.JSONStreamAware;

/**
* jqGrid 的数据源类
*
* @author rikugun
*
*/
public class JqGridDataSource implements JSONAware, JSONStreamAware {

private int records = 0;
private int page = 0;
private int total = 0;
private List rows = new LinkedList();
private JSONObject json = new JSONObject();

/**
* 添加一行记录
*
* @param row
* 输出的记录
* @return 当前数据源,方便链式写法 ds.add("a row").add("other row");
*/
public JqGridDataSource addRow(String row) {
rows.add(row);
return this;
}

/**
* 添加一行记录
*
* @param obj
* JSON对象,转换成字符串
* @return 当前数据源,方便链式写法 ds.add(obj1).add(obj2);
*/
public JqGridDataSource addRow(JSONObject obj) {
rows.add(obj.toJSONString());
return this;
}
/**
* 构造json对象
*/
private void build() {
json.put("records", new Integer(records));
json.put("page", new Integer(page));
json.put("total", new Integer(total));
json.put("rows", rows);
}

public int getRecords() {
return records;
}

public void setRecords(int records) {
this.records = records;
}

public int getPage() {
return page;
}

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

public int getTotal() {
return total;
}

public void setTotal(int total) {
this.total = total;
}

public List getRows() {
return rows;
}

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


public String toJSONString() {
build();
return json.toJSONString();
}

public void writeJSONString(Writer out) throws IOException {
build();
json.writeJSONString(out);
}

}




//usage
//..........
Map m = new LinkedHashMap();
m.put("name","rikugun");
m.put("phone","1323xxxxxxx");

JSONObject obj = new JSONObject();
obj.put("name","rikugun1");
obj.put("phone","1323xxxxxxx");

new JqGridDataSource ds = new JqGridDataSource();
ds.setPage(1);
ds.setTotal(1);
ds.add(JSONValue.toJSONString(m));
ds.add(obj);

System.out.println(ds.toJSONString());
//or

ds.writeJSONString(System.out);

//result {"page":1,"total":1,"records":2,"rows":[{"name":"rikugun","phone":"1323xxxxxxx"},{"name":"rikugun1","phone":"1323xxxxxxx"}]}


回头有空再写个1.5的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值