问题描述
我是 jqgrid 的新手,最后我设置了一个网格.假设我需要设置 jsonReader 以便网格知道在 json 返回中从哪里获取我的网格数据.但是尝试了几天后我得到了空白单元格.
I'm new to jqgrid finally i've setup a grid. Suppose i need to setup jsonReader so that the grid knows where to get my grid-data in the json return. However i got blank cells after trying for days.
这是我的网格:
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 返回的服务器:
This is the server return from url dbtest.aspx/get_offsite_history2:
{"d":"[{"name":"A","start_date":"B","duration":"C","offsite_cat":"D"}]"}
我想通过设置root:'d'"来获得结果,但我得到了 64 个空白行...
i suppose to get the result by setting "root: 'd'" but i got 64 blank rows for that...
寻找评论...非常感谢
look for comments... many thanks
推荐答案
您的问题的原因是您的服务器代码中的错误.您将序列化为 JSON 两次.在对服务器响应的 d
属性进行反序列化后,您仍然会得到 JSON 字符串 (!!!) 而不是对象.典型错误是在 web 方法中手动使用 JavaScriptSerializer.Serialize
.应该返回 object 本身而不是作为序列化结果的字符串.
The reason of your problem is the bug in your server code. You make serialization to JSON twice. After deserializing of d
property of the server response you get still JSON string (!!!) instead of object. Typical error is manual usage of JavaScriptSerializer.Serialize
in the web method. One should return the object itself instead of the string which is the result of serializing.
在不修改当前服务器代码的情况下,您可以通过使用来解决问题
Without modifying of your current server code you can fix the problem by usage of
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
)只是
or (if you use loadonce: true
) just
jsonReader: {
root: function (obj) {
return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
},
repeatitems: false
}
因为您当前的服务器代码似乎没有实现数据分页,您应该将 rowNum
增加到足够大的值,例如 rowNum: 10000
或使用 loadonce:是的
.
Because your current server code seems not implemented the paging of data you should increase rowNum
to some large enough value like rowNum: 10000
or to use loadonce: true
.
更新:您可以找到这里 修改后的演示效果.它显示
UPDATED: You can find here modified demo which works. It displays
在 alert
消息之后.
这篇关于jqgrid jsonReader 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!