2 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試通過(guò) Ajax 加載 DataTable,而不是在服務(wù)器上呈現(xiàn) html。
HTML
<table id="data-table" class="table table-striped table-bordered dt-responsive nowrap dataTable no-footer dtr-inline collapsed">
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>E-Mail</th>
<th>Action</th>
</tr>
<tfoot></tfoot>
</table>
JavaScript
const table = $('#customer-table').DataTable({
'processing': true,
'serverSide': true,
'ajax': {
'url': 'customers/list',
'type': 'POST'
},
'columns': [
{'data': 'id'},
{'data': 'first_name'},
{'data': 'last_name'},
{'data': 'email'},
{
'orderable': false,
'searchable': false,
'data': null,
'render': function (data, type, row, meta) {
// render custom html
return '<button type="button" class="btn btn-info">Edit</button>';
}
}
],
});
PHP
在服務(wù)器端獲取 POST 請(qǐng)求參數(shù)并構(gòu)建一個(gè)動(dòng)態(tài)查詢(使用 QueryBuilder)。
然后將結(jié)果集映射到 DataTable 兼容的 JSON 響應(yīng)中:
控制器動(dòng)作
// Build dynamic query
// ...
// Fetch result set
// ...
return response()->json([
'recordsTotal' => $count,
'recordsFiltered' => $count,
'draw' => $draw,
'data' => $rows,
];

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
public function renderPage() {
$customers = DB::table('users')->paginate(15);
return view('pages.admin.customers')->with([
'customers' => $customers
]);
}
- 2 回答
- 0 關(guān)注
- 163 瀏覽
添加回答
舉報(bào)