JQGrid例子关键片段
一、前台页面:
将以下两个Div放在Body标记中,table是数据的容器,div是工具栏
<DIV name="code">//定义列名称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方法}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"); //将数据加入listList 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);}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());JSONObjectjsonObject = JSONObject.fromObject(object, jsonConfig);log.debug(jsonObject.toString());out.print(jsonObject);} catch (IOException e) {log.debug(e);} finally {out.close();}}<package name="com.test" namespace="/brm" extends="json-default"> <action name="dirMgt_*" method="{1}"><result name="success"></result> </action></package>