JQGrid例子关键片段

一、前台页面:
将以下两个Div放在Body标记中,table是数据的容器,div是工具栏
<DIV class="ui-layout-center">
<table id="gridList"></table>
<div id="pagerBar"></div>
</DIV>

//js部分
//定义列名称
var jqgrid_colNames=['nodeCode','nodeName', 'domain'];
//定义列模型
var jqgrid_colModel = [
{
name:'nodeCode',
index:'nodeCode',
width:80,
align:"center",
editable:true
},{
name:'nodeName',
index:'nodeName',
width:80,
editable:true,
editoptions:{size:10},
required:true
},{
name:'domain',
index:'domain',
width:90,
editable:true,
editoptions:{size:25},
required:true
}
];

//初始化jqgrid,放在jquery的ready内
function jqgridInit () {
jQuery("#gridList").jqGrid({
url:'/ibrms/brm/dirMgt_testJQGrid.action',
datatype: "json",
colNames:jqgrid_colNames,
colModel:jqgrid_colModel,
rowNum:10,
rowList:[10,20,30],
pager: '#pagerBar',
sortname: 'nodeCode',
viewrecords: true,
sortorder: "desc",
caption:"子目录列表",
height:"400",
autowidth:true,
toolbar: [true,'top'],
editurl:"someurl.php",
jsonReader:{
root:"dataRows",
page: "currentPage",
total: "totalPages",
records: "records",
repeatitems: false
}
}).navGrid('#pagerBar',{ edit:true, add:true, del:true});

二、后台:
//将要转换为json对象的类
//为了方便,将jqgrid需要的参数封装了一个类。
public class JQGridResponse {
private int totalPages; //总页数
private int currentPage; //当前页码
private int records; //总记录数
private JSONArray dataRows;//JSONArray记录集合

public JQGridResponse() {
this.totalPages = 0;
this.currentPage = 0;
this.records = 0;
this.dataRows = new JSONArray();
}
//以下省略get/set方法
}

[b]Action[/b]:
public void testJQGrid(){
JQGridResponse grid= new JQGridResponse();
//构造数据
BrmNode brmNode1 = new BrmNode();
brmNode1.setNodeCode("001");
brmNode1.setNodeName("node1");
brmNode1.setDomain("ibrms");

BrmNode brmNode2 = new BrmNode();
brmNode2.setNodeCode("002");
brmNode2.setNodeName("node2");
brmNode2.setDomain("ibrms");

BrmNode brmNode3 = new BrmNode();
brmNode3.setNodeCode("003");
brmNode3.setNodeName("node3");
brmNode3.setDomain("ibrms");
//将数据加入list
List list = new ArrayList();
list.add(brmNode1);
list.add(brmNode2);
list.add(brmNode3);
//设置分页等参数
grid.setCurrentPage(1);
grid.setTotalPages(1);
grid.setRecords(3);
//将list转换为JSONArray对象
grid.setDataRows(JSONArray.fromObject(list));

ResponseWriteUtil.print2JsonObj(response, grid);
}


ResponseWriteUtil.print2JsonObj方法:

public static void print2JsonObj(HttpServletResponse response,Object object) {
PrintWriter out = null;
try {
out = response.getWriter();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Timestamp.class,new JsonValueProcessorTimestamp());
JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig);
log.debug(jsonObject.toString());
out.print(jsonObject);
} catch (IOException e) {
log.debug(e);
} finally {
out.close();
}
}

[b]Struts2配置[/b]:
<package name="com.test" namespace="/brm" extends="json-default">
<action name="dirMgt_*" class="com.test.action.DirMgtAction" method="{1}">
<result name="success"></result>
</action>
</package>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值