根据您的示例代码,jqGrid 将对
editurl: '/Service.svc/UpdateGrid'
您将需要在 WCF 服务上创建此方法。jqGrid 将调用 HTTP Post 并调用此方法,发送列值。
您将在 WCF 服务中添加与以下类似的操作:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json)]
public void UpdateGrid(string col1, string col2)
{
//code to actually do the update to the database
}
WCF 操作将负责实际定位数据库表中的正确行(我假设您可以根据存储在 col1 或 col2 中的值执行此操作)并执行更新操作。
下面的代码片段来自我的实现,使用您的示例作为起点:
[默认.html]
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.8.3.js" type="text/javascript"></script>
<script src="Scripts/jqGrid/grid.locale-en.js" type="text/javascript"></script>
<script src="Scripts/jqGrid/jquery.jqGrid.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.10.2.js"></script>
<link href="Content/ui.jqgrid.css" rel="stylesheet" />
<link href="Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript">
var lastSel;
$(document).ready(function() {
var service = 'http://localhost:5127/Service.svc/GetData';
$("#MyGrid").jqGrid({
url: service,
datatype: "json",
height: 255,
width: 600,
colNames: ['Col1', 'Col2'],
colModel: [
{ name: 'Col1', index: 'Col1', sortable: true, resizable: true, editable: false, sorttype: "text" },
{ name: 'Col2', index: 'Col2', align: 'left', sortable: true, resizable: true, width: 50, editable: true },
],
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
cell: "",
repeatitems: false
},
rowNum: 1000,
rowList: [10, 30, 100, 1000],
pager: '#pagerLab',
sortname: 'Col1',
viewrecords: true,
gridview: true,
loadonce: true,
onSelectRow: function (id) {
if (id && id !== lastSel) {
$(this).restoreRow(lastSel);
lastSel = id;
}
jQuery(this).editRow(id, true);
},
editurl: 'http://localhost:5127/Service.svc/UpdateData',
ajaxRowOptions: { contentType: 'application/json; charset=utf-8' },
serializeRowData: function (data) {
return JSON.stringify(data);
}
});
jQuery("#MyGrid").jqGrid('navGrid', "#pagerLab",
{ edit: false, add: false, del: false, search: false }
);
jQuery("#MyGrid").jqGrid('inlineNav', "#pagerLab");
});
</script>
</head>
<body>
<table id="MyGrid"></table>
<div id="pagerLab"></div>
</body>
</html>
[IService.cs]
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method="GET", BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json)]
jqGridModel<GridListItem> GetData();
[OperationContract]
[WebInvoke(Method="POST", BodyStyle=WebMessageBodyStyle.WrappedRequest, RequestFormat=WebMessageFormat.Json)]
void UpdateData(string id, string oper, string Col1, string Col2);
}
[DataContract]
public class GridListItem
{
[DataMember]
public string Col1 { get; set; }
[DataMember]
public string Col2 { get; set; }
}
[DataContract]
public class jqGridModel<T>
{
public jqGridModel()
{
this.total = 0;
this.rows = null;
this.records = 0;
this.page = 0;
}
[DataMember]
public int total { get; set; }
[DataMember]
// this is the property where you store the actual data model
public IList<T> rows { get; set; }
[DataMember]
public int page { get; set; }
[DataMember]
public int records { get; set; }
}
}
[服务.svc.cs]
public class Service : IService
{
jqGridModel<GridListItem> IService.GetData()
{
jqGridModel<GridListItem> model = new jqGridModel<GridListItem>();
List<GridListItem> list = new List<GridListItem>();
GridListItem item = new GridListItem() { Col1 = "1", Col2 = "Dog" };
list.Add(item);
item = new GridListItem() { Col1 = "2", Col2 = "Cat" };
list.Add(item);
model.records = list.Count;
model.rows = list;
return model;
}
void IService.UpdateData(string id, string oper, string Col1, string Col2)
{
//do work here to save the updated data
}
}