我是jqgrid的新手,最后我设置了一个网格。假设我需要设置jsonReader,以便网格知道在json返回中从哪里获取网格数据。然而,在尝试了几天之后,我得到了空白单元格。
这是我的网格:
jQuery("#list48").jqGrid({
url: 'dbtest.aspx/get_offsite_history2',
datatype: "json",
mtype: 'POST',
ajaxGridOptions: { contentType: "application/json" },
serializeGridData: function(postData) {
return JSON.stringify(postData);
},
jsonReader: {
root: function(obj) { alert(JSON.stringify(obj.d)); return obj.d; },
repeatitems: false
},
height: 'auto',
rowNum: 30,
rowList: [10, 20, 30],
colNames: ['name', 'start_date', 'duration', 'offsite_cat'],
colModel: [
{ name: 'name', index: 'name', width: 80, align: 'left', editable: true, edittype: 'text' },
{ name: 'start_date', index: 'start_date', width: 120, align: 'left', editable: true, edittype: 'text' },
{ name: 'duration', index: 'duration', width: 120, align: 'left', editable: true, edittype: 'text' },
{ name: 'offsite_cat', index: 'offsite_cat', width: 120, align: 'left', editable: true, edittype: 'text'}],
pager: "#plist48",
viewrecords: true,
sortname: 'name',
caption: "Grouping Array Data",
gridview: true
});这是从url dbtest.aspx/get_offsite_history2返回的服务器:
{"d":"[{\"name\":\"A\",\"start_date\":\"B\",\"duration\":\"C\",\"offsite_cat\":\"D\"}]"}我想通过设置"root:'d'“来获得结果,但是我得到了64个空行...
查找评论...非常感谢
发布于 2013-02-07 18:49:52
问题的原因是服务器代码中的bug。您对JSON 进行了两次序列化。在对服务器响应的d属性进行反序列化之后,您得到的仍然是JSON字符串(!)而不是对象。典型的错误是在web方法中手动使用JavaScriptSerializer.Serialize。应该返回对象本身,而不是作为序列化结果的字符串。
在不修改当前服务器代码的情况下,可以使用
jsonReader: {
root: function (obj) {
alert(typeof obj.d === "string" ? obj.d : JSON.stringify(obj.d));
return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
},
repeatitems: false,
page: function () { return 1; },
total: function () { return 1; },
records: function (obj) {
return typeof obj.d === "string" ? $.parseJSON(obj.d).length : obj.length;
}
}或者(如果您使用loadonce: true)
jsonReader: {
root: function (obj) {
return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
},
repeatitems: false
}因为您当前的服务器代码似乎没有实现数据分页,所以您应该将rowNum增加到某个足够大的值,比如rowNum: 10000,或者使用loadonce: true。
更新了:你可以找到here修改过的演示。它会显示

在alert消息之后。
发布于 2013-02-07 18:22:42
我认为问题出在返回的json数据的结构上。
下面是我使用的一个:
{ "page":1,
"rows":[{"id":"1","cell":["1","1","Row 1","3","9",""]},
{"id":"2","cell":["2","2","Row 2","2","1",""]},
{"id":"3","cell":["3","4","Row 3","2","0",""]}],
"records":3,
"total":1
}您可能需要为id添加一个colModel,以便唯一地标识每一行。
例如:
colNames: ['id', 'name', 'start_date', 'duration', 'offsite_cat'],
colModel: [
{ name: 'id', index: 'id', hidden: true },
{ name: 'name', index: 'name', width: 80, align: 'left', editable: true, edittype: 'text' },
{ name: 'start_date', index: 'start_date', width: 120, align: 'left', editable: true, edittype: 'text' },
{ name: 'duration', index: 'duration', width: 120, align: 'left', editable: true, edittype: 'text' },
{ name: 'offsite_cat', index: 'offsite_cat', width: 120, align: 'left', editable: true, edittype: 'text'}],希望这能有所帮助。
https://stackoverflow.com/questions/14748169
复制相似问题