我需要用从 ajax 调用收到的数据填充我的 jqGrid。这是我的 jqGrid 代码:
$("#discuss_table_answers").jqGrid({
datatype: "local",
jsonReader: {
root: "list",
repeatitems: false,
records: "size"
},
colNames: ['agentId', 'date', 'deleted', 'id', 'questionId', 'rating', 'reported', 'text'],
colModel: [
{name: 'agentId', index: 'agentId', width: 80, align: 'right', formatter:"number"},
{name: 'date', index: 'date', width: 80, align: 'right', formatter:"date"},
{name: 'deleted', index: 'deleted', width: 150, sortable: true
, formatter: function myformatter(cellvalue, options, rowObject) {
if (cellvalue) {
return "<p>Rejected</p>";
}
return "<button class='reject_question' style='height: 20px;width: 100px'>Reject</button>";
}
},
{name: 'id', index: 'id', width: 55,formatter:"number"},
{name: 'questionId', index: 'questionId', width: 90,formatter:"number"},
{name: 'rating', index: 'rating', width: 150, sortable: false,formatter:"number"},
{name: 'reported', index: 'reported', width: 150, sortable: false},
{name: 'text', index: 'text', width: 80, align: 'right'}
], height: "100%" });
添加数据的功能代码:
function addData (rowId) {
var ansTable = $("#discuss_table_answers");
ansTable.clearGridData(true);
$.getJSON("review2-admin/question/" + rowId + "/answers", function (data) {
console.log(data);
console.log(data['list'].length);
ansTable.setGridParam({data: data}).trigger('reloadGrid');
});
从萤火虫我看到我从服务器得到了正确的答案:
{"size":2,"list":[{"id":15,"questionId":9,"agentId":7327516,"text":"лоол","date":1364882946000,"reported":false,"deleted":false,"rating":0},{"id":12,"questionId":9,"agentId":4405127,"text":"456456456","date":1364793241000,"reported":false,"deleted":false,"rating":2}]}
所以问题是从服务器获取的数据没有显示在网格中。
我尝试使用类似的测试数据var testData = [
{list: {agentId: "7327516", date: "1364882946000", deleted: false, id: "15", questionId: "9", rating: "0",
reported: false, text: "лоол"}, size: 1}
];
,它工作正常,表格刷新了。