$(
function()
{
$(
"#gridTable").jqGrid({
url:'jqGridServlet',
datatype:
"json",
height: 250,
colNames:['編號','用戶名', '性別', '郵箱', 'QQ','手機號','出生日期'],
colModel:[
{name:'id',index:'id', sorttype:
"int"},
{name:'userName',index:'userName',
{name:'gender',index:'gender',
{name:'email',index:'email', ;string"},
{name:'QQ',index:'QQ', ;
{name:'mobilePhone',index:'mobilePhone', ;
{name:'birthday',index:'birthday', sorttype:
"date"}
],
sortname:'id',
sortorder:'asc',
viewrecords:
true,
rowNum:10,
rowList:[10,20,30],
jsonReader:{
repeatitems :
false
},
pager:
"#gridPager",
caption:
"jqGrid與Servlet集成"
}).navGrid('#gridPager',{edit:
false,add:
false,del:
false});
})
JavaScript的代碼跟以前有幾個地方不同。其中,url:'jqGridServlet'的jqGridServlet是Servlet的 url-pattern;datatype: "json"表示服務器端須要返回json數據;jsonReader:{repeatitems : false}則是爲了後臺處理數據方便而設置,具體意思見下面Java代碼的註釋。
package com.polaris.jqgrid.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* 該Servlet向客戶端返回一個json對象。爲了簡便,數據不是從數據庫得到的。
* jqGrid默認指望返回的json對象格式要求以下:
* {"page":"1","total":"2","records":"13",
* "rows":[
* {id:"1",cell:["1","polaris","男","polaris@gmail.com","772618379","18329382732","1985-10-2"]},
* {id:"2",cell:["2","張三","女","zhangsan@163.com","272618382","15329382732","1986-10-12"]},
* {id:"3",cell:["3","王五","女","wangwu@yahoo.com","172635372","13329389832","1987-12-21"]},
* {id:"4",cell:["4","趙六","男","zhaoliu@sina.com","372618332","18929343731","1988-09-22"]}
* ]
* }
* 固然,在js中,能夠經過jqGrid的jsonReader屬性來修改默認格式
* 由於默認的格式,rows的數據要求順序不能變,且每一個字段都得有值(空也得有"")。於是,
* 在jsonReader中定義repeatitems : false。這樣,rows就變成了:
* "rows":[
* {id:"1",userName:"polaris",gender:" 男",email:"polaris@gmail.com",QQ:"772618379",mobilePhone:"18329382732",birthday:"1985-10-2"]},
* {id:"2",userName:"徐新華",gender:" 男",email:"xh.xu@163.com",QQ:"272618382",mobilePhone:"15329382732",birthday:"1986-10-12"]},
* {id:"3",userName:"王五",gender:" 女",email:"wangwu@yahoo.com",QQ:"172635372",mobilePhone:"13329389832",birthday:"1987-12-21"]},
* {id:"4",userName:"趙六",gender:" 女",email:"zhaoliu@sina.com",QQ:"372618332",mobilePhone:"18929343731",birthday:"1988-09-22"]}
* ]
* @author xuxinhua
*
*/
public
class JqGridForJSONServlet
extends HttpServlet
{
private
static
final
long serialVersionUID = 132383828833L;
@Override
public
void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 定義返回的數據類型:json,使用了json-lib
JSONObject jsonObj =
new JSONObject();
// 根據jqGrid對JSON的數據格式要求給jsonObj賦值
jsonObj.put(
"page", 1);
// 當前頁
jsonObj.put(
"total", 1);
// 總頁數
jsonObj.put(
"records", 4);
// 總記錄數
// 定義rows,存放數據
JSONArray rows =
new JSONArray();
// 放入4條數據
for(
int i=0;i<4;i++)
{
// 存放一條記錄的對象
JSONObject cell =
new JSONObject();
cell.put(
"id", i);
if(i%2==0)
{
cell.put(
"userName",
"polaris");
cell.put(
"gender",
"女");
}
else
{
cell.put(
"userName",
"徐新華");
cell.put(
"gender",
"男");
}
cell.put(
"email",
"polaris@gmail.com");
cell.put(
"QQ",
"772"+i+
"1837"+i);
cell.put(
"mobilePhone",
"132"+i+
"1837"+i+
"3"+i);
cell.put(
"birthday",
"198"+i+
"-10-"+
"1"+i);
// 將該記錄放入rows中
rows.add(cell);
}
// 將rows放入json對象中
jsonObj.put(
"rows", rows);
// 自控制檯打印輸出,以檢驗json對象生成是否正確
System.out.println(
"要返回的json對象:\n" + jsonObj.toString());
// 設置字符編碼
resp.setCharacterEncoding(
"UTF-8");
// 返回json對象(經過PrintWriter輸出)
resp.getWriter().print(jsonObj);
}
@Override
public
void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}