这个例子展示了使表格可编辑的基本步骤。这样做我们只需要3步:
- 标记Grid的哪一列是可编辑的。这是通过设置一个新属性"editable:true"实现的。
默认grid中所有咧都是不可编辑的。 - 指定服务器需要接收发送的编辑数据的url。这是通过设置"editurl"选项来实现的。
- 使用这三个方法:editRow, saveRow, restoreRow。
editRow(rowid, keys) -接收两个参数:rowid-行的唯一id和列名。当我们调用此方法之后,我们可以使用Enter按键和Esc按键来保存和取消编辑状态。
saveRow( rowid, callback, url, extraparams) - rowid:行的唯一id,callback:自定义方法,他是当我们想服务器发送数据之后来接收数据的方法。url:当我们指定了此参数,我们就会覆盖editurl参数。extraparams :添加我们想服务器发送的一些参数。当这些数据格式化成 id=rowid&name=value…的时候,里面的name就是colModel的中的那么字段。
restoreRow( rowid) -在行编辑之前回复数据到初始值。
提示:由于安全原因,数据不会保存到服务器。
HTML代码举例
<html> <head> <title>jqGrid 实例</title> </head> <body> ···代码省略··· <table id="rowed1"></table> <div id="prowed1"></div> <br /> <input type="BUTTON" id="ed1" value="Edit row 13" /> <input type="BUTTON" id="sved1" disabled='true' value="Save row 13" /> <input type="BUTTON" id="cned1" disabled='true' value="Cancel Save" /> ···代码省略··· </body> </html>
javascript代码举例
$(function(){ pageInit(); }); function pageInit(){ jQuery("#rowed1").jqGrid( { url : ctx+'/JSONData', datatype : "json", colNames : [ 'Inv No', 'Date', 'Client', 'Amount', 'Tax','Total', 'Notes' ], colModel : [ {name : 'id',index : 'id',width : 55}, {name : 'invdate',index : 'invdate',width : 90,editable : true}, {name : 'name',index : 'name',width : 100,editable : true}, {name : 'amount',index : 'amount',width : 80,align : "right",editable : true}, {name : 'tax',index : 'tax',width : 80,align : "right",editable : true}, {name : 'total',index : 'total',width : 80,align : "right",editable : true}, {name : 'note',index : 'note',width : 150,sortable : false,editable : true} ], rowNum : 10, rowList : [ 10, 20, 30 ], pager : '#prowed1', sortname : 'id', viewrecords : true, sortorder : "desc", editurl : ctx+"/RowEditing", caption : "Basic Example" }); jQuery("#rowed1").jqGrid('navGrid', "#prowed1", { edit : false, add : false, del : false }); jQuery("#ed1").click(function() { jQuery("#rowed1").jqGrid('editRow', "13"); this.disabled = 'true'; jQuery("#sved1,#cned1").attr("disabled", false); }); jQuery("#sved1").click(function() { jQuery("#rowed1").jqGrid('saveRow', "13"); jQuery("#sved1,#cned1").attr("disabled", true); jQuery("#ed1").attr("disabled", false); }); jQuery("#cned1").click(function() { jQuery("#rowed1").jqGrid('restoreRow', "13"); jQuery("#sved1,#cned1").attr("disabled", true); jQuery("#ed1").attr("disabled", false); }); }
java servlet代码举例
public class JSONData extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public JSONData() { super(); // TODO Auto-generated constructor stub } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req,resp); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String jsondata = "{\"page\":\"1\"," + " \"total\":2," + " \"records\":\"13\"," + " \"rows\":" + " [" + " {" + " \"id\":\"13\"," + " \"cell\":" + " [\"13\",\"2007-10-06\",\"Client 3\",\"1000.00\",\"0.00\",\"1000.00\",null]" + " }," + " {" + " \"id\":\"12\"," + " \"cell\":" + " [\"12\",\"2007-10-06\",\"Client 2\",\"700.00\",\"140.00\",\"840.00\",null]" + " }," + " {" + " \"id\":\"11\"," + " \"cell\":" + " [\"11\",\"2007-10-06\",\"Client 1\",\"600.00\",\"120.00\",\"720.00\",null]" + " }," + " {" + " \"id\":\"10\"," + " \"cell\":" + " [\"10\",\"2007-10-06\",\"Client 2\",\"100.00\",\"20.00\",\"120.00\",null]" + " }," + " {" + " \"id\":\"9\"," + " \"cell\":" + " [\"9\",\"2007-10-06\",\"Client 1\",\"200.00\",\"40.00\",\"240.00\",null]" + " }," + " {" + " \"id\":\"8\"," + " \"cell\":" + " [\"8\",\"2007-10-06\",\"Client 3\",\"200.00\",\"0.00\",\"200.00\",null]" + " }," + " {" + " \"id\":\"7\"," + " \"cell\":" + " [\"7\",\"2007-10-05\",\"Client 2\",\"120.00\",\"12.00\",\"134.00\",null]" + " }," + " {" + " \"id\":\"6\"," + " \"cell\":" + " [\"6\",\"2007-10-05\",\"Client 1\",\"50.00\",\"10.00\",\"60.00\",\"\"]" + " }," + " {" + " \"id\":\"5\"," + " \"cell\":" + " [\"5\",\"2007-10-05\",\"Client 3\",\"100.00\",\"0.00\",\"100.00\",\"no tax at all\"]" + " }," + " {" + " \"id\":\"4\"," + " \"cell\":" + " [\"4\",\"2007-10-04\",\"Client 3\",\"150.00\",\"0.00\",\"150.00\",\"no tax\"]" + " }" + " ]," + " \"userdata\":{\"amount\":3220,\"tax\":342,\"total\":3564,\"name\":\"Totals:\"}" + " }"; response.getWriter().write(jsondata); } }