这2个月刚换单位,比较赶进度.很久不更新blog,今天抽空看了一下jqGrid,因为现在工作环境又迁到java了,但是是1.4的呵呵,所以封装了一个jqGrid的数据源,方便使用
ps: 需要json-simple
点我
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的