在jqGrid的官方文档http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules中,对jqGrid的编辑总结了以下三种类型的编辑:
在jqGrid中主要通过以下colModel参数来实现:
edittype="text", editoptions: {size:10, maxlength: 15} edittype="textarea", editoptions: {rows:"2",cols:"10"} edittype="textarea",editoptions: { value:"Yes:No" } edittype="select",editoptions: { value: “FE:FedEx; IN:InTime; TN:TNT” }//对于多选的支持:editoptions: {multiple:true, size:3... } //当edittype为custom时需要有两个函数,一个用来创建该元素(custom_element另外一个对其赋值(custom_value),如 edittype:'custom', editoptions:{ custom_element: function(value, options){ }, custom_value:function(elem, operation, value){ } }
editrules:{custom:true, custom_func: function(value, colname){ if (value < 0 || value >20) { return [false,"Please enter value between 0 and 20"]; }else{ return [true,""]; } }}
{ 'cellEdit' : true, 'cellsubmit' : 'remote|clientArray', 'cellurl' : '/url/to/handling/the/changed/cell/value' }
var datas = [ {"id":1, "name":"name1", "date":"2012-08-18 11:11:11", "price":123.1, "email":"[email protected]", "amount":1123423, "gender":"1", "type":"0"}, {"id":2, "name":"name2", "date":"2012-08-18 11:11:11", "price":1452.2, "email":"[email protected]", "amount":12212321, "gender":"1", "type":"1"}, {"id":3, "name":"name3", "date":"2012-08-18 11:11:11", "price":125454, "email":"[email protected]", "amount":2345234, "gender":"0", "type":"0"}, {"id":4, "name":"name4", "date":"2012-08-18 11:11:11", "price":23232.4, "email":"[email protected]", "amount":2345234, "gender":"1", "type":"2"}, {"id":5, "name":"name5", "date":"2012-08-18 11:11:11", "price":473454, "email":"[email protected]", "amount":5234534, "gender":"0", "type":"0"}, {"id":6, "name":"name6", "date":"2012-08-18 11:11:11", "price":34563, "email":"[email protected]", "amount":2345342, "gender":"1", "type":"1"} ];
colModel:[ {name:'id', index:'id'}, {name:'name', index:'name', editable:true, edittype:"text", editrules:{required: true},editoptions:{size:16, maxlength: 15}}, {name:'date', index:'date', editable:true, edittype:"text", editrules:{date: true},editoptions:{ size: 10, maxlengh: 10, dataInit: function(element) { $(element).datepicker({dateFormat: 'yy-mm-dd'}); } }}, {name:'price', index:'price', editable:true, edittype:"text", editrules:{required: true, number: true}}, {name:'email', index:'email', editable:true, edittype:"text", editrules:{required: true, email: true}}, {name:'amount', index:'amount', editable:true, edittype:"text", editrules:{required: true, number: true, maxValue:20}}, {name:'gender', index:'gender', editable:true, edittype:"checkbox", editrules:{value:"Yes:No"}}, {name:'type', index:'type', editable:true, edittype:'select', formatter:'select', editoptions:{value:"0:现货;1:可配货;2:无货"}} ],
ondblClickRow: function(id){ if(id && id !== lastsel){ var rowData = $("#jqGridId").jqGrid("getRowData", id); $('#jqGridId').jqGrid('restoreRow',lastsel); $('#jqGridId').jqGrid('editRow',id,{ keys : true, //这里按[enter]保存 url: s2web.appURL + "jq/save.action", mtype : "POST", restoreAfterError: true, extraparam: { "ware.id": rowData.id, "ware.warename": $("#"+id+"_name").val(), "ware.createDate": $("#"+id+"_date").val(), "ware.number": $("#"+id+"_amount").val(), "ware.valid": $("#"+id+"_type").val() }, oneditfunc: function(rowid){ console.log(rowid); }, succesfunc: function(response){ alert("save success"); return true; }, errorfunc: function(rowid, res){ console.log(rowid); console.log(res); } }); } }以上是在双击grid的row时触发当前row为可编辑状态,在回车时调用editRow对当前编辑数据进行保存。展现的效果如下:
jQuery("#grid_id").jqGrid('editRow',rowid, keys, oneditfunc, succesfunc, url, extraparam, aftersavefunc,errorfunc, afterrestorefunc);或者:
jQuery("#grid_id").jqGrid('editRow',rowid, { "keys" : false, "oneditfunc" : null, "successfunc" : null, "url" : null, "extraparam" : {}, "aftersavefunc" : null, "errorfunc": null, "afterrestorefunc" : null, "restoreAfterError" : true, "mtype" : "POST" });
$("#addBtn").bind("click", function() { $("#jqGridId").jqGrid('addRow',{ rowID : "new_row", initdata : {}, position :"first", useDefValues : true, useFormatter : true, addRowParams : {extraparam:{ }} }); //当前新增id进入可编辑状态 $('#jqGridId').jqGrid('editRow','new_row',{ keys : true, //这里按[enter]保存 url: s2web.appURL + "jq/save.action", mtype : "POST", restoreAfterError: true, extraparam: { }, oneditfunc: function(rowid){ console.log(rowid); }, succesfunc: function(response){ alert("save success"); return true; }, errorfunc: function(rowid, res){ console.log(rowid); console.log(res); } }); });
$("#grid_id").jqGrid('editGridRow', rowid, {properties} );
$("#grid_id")..jqGrid('editGridRow', "new", properties );