我想通过ajax加载数据表,如link
我的ajax数据源是这样的
$result = DB::table('test')->get();
$test = [];
$i=0;
foreach ($result as $row):
$test[$i]['id'] = "-";
$test[$i]['kode'] = $row->kode;
$test[$i]['date'] = $row->date;
$test[$i]['name'] = $row->name;
$test[$i]['total'] = $row->total;
$test[$i]['payment'] = $row->payment;
$i++;
endforeach;
return json_encode(['aaData' => $test]);
但我无法访问初始化数据表中的数据
var dTable = $('#dataTable').DataTable({
"processing": true,
"serverSide": true,
"sAjaxSource": "{{ URL::route('list.laporan.pembelian') }}",
"columnDefs": [
{ "targets": 0, "className": 'details-control', "orderable": false, "data": null, "defaultContent": '' },
{ "data": "kode", "name" : "kode" },
{ "data": "date", "name" : "date" },
{ "data": "name", "name" : "name" },
{ "data": "total", "name" : "total" },
{ "data": "payment", "name" : "payment" }
]
});
如果使用此脚本<{1}}成功加载数据,我试图打印
console.log
并且它会像这样给出结果
dTable.on( 'xhr', function ( e, settings, json ) {
console.log( 'Ajax event occurred. Returned data: ', json );
});
我认为这意味着我已成功加载数据。
但为什么我仍然无法访问它?
更新@ivar
答案 0 :(得分:1)
我不认为你以正确的方式返回数据。请查看数据表网站的Server-side processing page。
您应该返回的数据应为:
draw
计数器 - 来自作为数据请求的一部分发送的draw参数。)我将添加如何使用Ajax实现它:
表:
<table id="dataTables-table">
<thead>
<tr>
<th id="col1">col1name</th>
<th id="col2">col2name</th>
<th id="col2">col2name</th>
</tr>
</thead>
<tbody></tbody>
</table>
使用Javascript:
$('#dataTables-table').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"type": "POST",
"url": "url/to/page"
},
"columns": [
{ "name": "col1", "data": "col1" },
{ "name": "col1", "data": "col1" },
{ "name": "col1", "data": "col1" }
]
});