---
title: "JQGrid例子关键片段"
author: "bluebreath"
date: "2011-11-03T16:10:21Z"
tags: []
categories: ["Web前端"]
slug: "jqgrid"
draft: false
lastmod: "2011-11-03T16:10:21Z"
id: 1233543
url: "https://breath.iteye.com/blog/1233543"
---

一、前台页面： 将以下两个Div放在Body标记中，table是数据的容器，div是工具栏 \[code="java"]

\[/code] //js部分 \[code="java"]//定义列名称 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}); \[/code] 二、后台： \[code="java"]//将要转换为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方法 }\[/code] \[b]Action\[/b]： \[code="java"]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); }\[/code] ResponseWriteUtil.print2JsonObj方法： \[code="java"]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(); } } \[/code] \[b]Struts2配置\[/b]： \[code="java"] \[/code]