以下は、表示に使用される JSON データと JQGrid コードです。
JSON DATA * [{"Id":1,"Name":"トマトスープ","Category":"食料品","Price":1.0},{"Id":2,"Name":"ヨーヨー","Category":"Toys","Price":3.75},{"Id":3,"Name":"ハンマー","Category":"Hardware","Price":16.99}]
**JQGrid Code**
function getData() {
$.ajax({
//url: 'api/products',
data:'{}',
dataType: "json",
complete: function (jsondata, stat) {
if (stat == "success") {
var thegrid = jQuery("#gdCustomers")[0];
//var jdata = JSON.parse(eval("(" + jsondata.responseText + ")"));
thegrid.addJSONData(JSON.parse('[{"Id":1,"Name":"Tomato Soup","Category":"Groceries","Price":1.0},{"Id":2,"Name":"Yo-yo","Category":"Toys","Price":3.75},{"Id":3,"Name":"Hammer","Category":"Hardware","Price":16.99}]'));
}
}
});
}
'use strict';
gdCustomers.jqGrid({
//url: 'api/products',
datatype: getData,
mtype: 'GET',
loadError: function (xhr, status, error) { alert(status + " " + error); },
//ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
//serializeGridData: function (postData) {
// return JSON.stringify(postData);
//},
//jsonReader: {
// //root: '',
// //page: 1,//obj.d.page; },
// //total: 1,//obj.d.total; },
// //records: 3,//; }
// //rows: [],
// id: '0',
// cell:'',
// //repeatitems: true
//},
colNames: ["Id", "Name", "Category", "Price"],
colModel: [
{ name: "Id", index: "Id",key:true, width: 50 },
{ name: "Name", index: "Name", width: 200},
{ name: "Category", index: "Category", width: 75 },
{ name: "Price", index: "Price", width: 75}
],
rowNum:10,
rowList:[10,20,30],
pager: '#gdCustomersPager',
//sortname: 'id',
viewrecords: true,
//sortorder: "desc",
width:500,
height: 200,
caption: "JSON Example",
onCellSelect: function (rowid, index, contents, event) {
alert('onCellSelect: ' + index + ' : ' + contents);
}
});
jQuery("#gdCustomers").jqGrid('navGrid','#gdCustomersPager',{edit:false,add:false,del:false});
});
サーバー側コード Web API で asp.net を使用しています
public class ProductsController : ApiController {
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1},
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M},
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M}
};
public dynamic GetAllProducts()
{
return JsonConvert.SerializeObject(products);
}