我正在尝试从ajax响应加载datatable,然后执行服务器端处理。使用this example
这是我从服务器收到的回复:
{"msg":null,"code":null,"status":null,"result":[{"aNumber":"3224193861","bNumber":"3215910681","dateTime":"2017-06-05 09:44:22.0","duration":778,"imei":"47350901163665"},{"aNumber":"3224193861","bNumber":"3028540439","dateTime":"2017-04-26 18:53:23.0","duration":266,"imei":"31489802062929"}],"draw":1,"limit":1000,"recordsFiltered":13419,"recordsTotal":13419}
这是我用来处理ajax和datatable的javascript代码。
function showDataTable(anumber, startdate, enddate) {
var cdrReqParams = {};
cdrReqParams.draw = '1';
cdrReqParams.offset = 0;
cdrReqParams.newRequest = '1';
cdrReqParams.totalRecords = '1';
cdrReqParams.lookInCol = 'aNumber';
cdrReqParams.lookInVal = anumber;
cdrReqParams.fromDate = startdate;
cdrReqParams.toDate = enddate;
var jsonStr = JSON.stringify(cdrReqParams);
console.log(jsonStr);
API.call("http://localhost:8050/phpservice/json.php", 'POST', function(data) {
basicData = data.result;
console.log(basicData);
oTable = $("#table").dataTable({
bJQueryUI: true,
bPaginate: true,
sPaginationType: "full_numbers",
bFilter: false,
bInfo: false,
bProcessing: true,
bServerSide: true,
aaData: [basicData],
aoColumns: [{
"sTitle": "ANUMBER",
"mData": "aNumber"
}, {
"sTitle": "BNUMBER",
"mData": "bNumber"
}, {
"sTitle": "DATETIME",
"mData": "dateTime"
}, {
"sTitle": "DURATION",
"mData": "duration"
}, {
"sTitle": "IMEI",
"mData": "imei"
}]
});
}, function(error) {
console.log(error);
}, jsonStr);
}
通过这样做,我收到2个错误
DataTables警告:table id = table - 请求的未知参数 第0行第0列的“aNumber”。有关此错误的详细信息, 请参阅http://datatables.net/tn/4
和
无效的JSON响应。
是否有针对此类问题的解决方法,首先,您将执行ajax调用,并从接收到的数据中,您将使用服务器端处理填充数据表?
我希望有人会给我一个暗示。
答案 0 :(得分:0)
使用datatable 1.10(我认为您可能使用的是早期版本),您可以使用columns.data属性填充和格式化单元格中的数据:
columns: [
{ data: "", defaultContent: " " },
{ data: null,
defaultContent: " ",
render: function (data, type, row, meta) {
return data.ID;
}
}
]