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方法
}

Action
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();
		}
	}

Struts2配置
<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>

你可能感兴趣的:(jqGrid)