我正在使用jQuery的WebPart页面,我计划将jqGrid合并到页面中。该页面将加载grid.i得到的ajax响应中的所有列表项,但数据不会显示在网格中,它将显示消息“无记录视图”
jQuery("#list2").jqGrid({
url:"https://xyz.sharepoint.com/sites/Live/_vti_bin/ListData.svc/Region",
datatype: "json",
type: "GET",
contentType: 'application/json;odata=verbose',
ajaxGridOptions: { contentType: "application/json; charset=utf-8" },
colNames:["Country", "State", "City"],
colModel:[
{name:'Country',index:'Country', width:55},
{name:'State',index:'State', width:90},
{name:'City',index:'City', width:90}
],jsonReader : {
records: "__metadata",
cell: "",
repeatitems: false
},
rowNum: 3,
gridview: true,
pager: '#pager2',
autoencode: true,
viewrecords: true,
height: "auto",
ignoreCase: true,
hidegrid: false
});
</Script>
这是我获得的Json,
{
"d" : {
"results": [
{
"__metadata": {
"uri": "https://xyz.sharepoint.com/sites/Live/_vti_bin/ListData.svc/Region(1)", "etag": "W/\"1\"", "type": "Microsoft.SharePoint.DataService.RegionItem"
}, "ContentTypeID": "0x010071297C85CCC1654A942D938B605256CA", "Country": "Australia", "State": "New South Wales", "City": "Abbotsford", "Id": 1, "ContentType": "Item", "Modified": "\/Date(1397547577000)\/", "Created": "\/Date(1397547577000)\/", "CreatedBy": {
"__deferred": {
"uri": "https://xyz.sharepoint.com/sites/Live/_vti_bin/ListData.svc/Region(1)/CreatedBy"
}
}, "CreatedById": 9, "ModifiedBy": {
"__deferred": {
"uri": "https://xyz.sharepoint.com/sites/Live/_vti_bin/ListData.svc/Region(1)/ModifiedBy"
}
}, "ModifiedById": 9, "Owshiddenversion": 1, "Version": "1.0", "Attachments": {
"__deferred": {
"uri": "https://xyz.sharepoint.com/sites/Live/_vti_bin/ListData.svc/Region(1)/Attachments"
}
}, "Path": "/sites/live/Lists/Region"
},.....
让我知道我做错了什么。 任何帮助将受到高度赞赏。感谢。
答案 0 :(得分:6)
我正在回答我自己的问题,以防有人碰到类似的事情。这是对MS SharePoint站点的ajax调用,返回JSON中的列表数据。只有一件事在jsonReader中缺少必须指定“d.result”而不是“_metadata”。所以最终完整的java脚本看起来像这样。 / p>
jQuery("#list2").jqGrid({
url:"https://xyz.sharepoint.com/sites/Live/_vti_bin/ListData.svc/Region",
datatype: "json",
type: "GET",
contentType: 'application/json;odata=verbose',
ajaxGridOptions: { contentType: "application/json; charset=utf-8" },
colNames:["Country", "State", "City"],
colModel:[
{name:'Country',index:'Country', width:55},
{name:'State',index:'State', width:90},
{name:'City',index:'City', width:90}
],
jsonReader : {
records: "d.results",
cell: "",
repeatitems: false
},
rowNum: 3,
gridview: true,
pager: '#pager2',
autoencode: true,
viewrecords: true,
height: "auto",
ignoreCase: true,
hidegrid: false
});
</Script>
现在,这样可以正常工作,列表数据在jqGrid中成功加载。