【发布时间】:2019-06-18 18:02:07
【问题描述】:
我找不到这样的问题。所有其他问题都没有像我一样使用 Bootstrap 数据表 - 他们建立了自己的表。
我的 Laravel 5.8 应用程序当前返回可搜索数据表中的用户列表。问题是,它一次返回所有用户,因此页面加载速度非常慢,因为应用程序有很多用户。
我的routes\web.php:
Route::get('/admin/customers', 'Admin\CustomerController@renderPage')->name('admin.customers');
我的app\Http\Controllers\Admin\CustomerController.php:
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use ConsoleTVs\Charts\Facades\Charts;
use App\User;
class CustomerController extends Controller
{
public function renderPage() {
$customers = User::get();
return view('pages.admin.customers')->with([
'customers' => $customers
]);
}
}
我在视图resources\views\pages\admin\customers.blade.php 中的表格是这样生成的(我已经删除了不相关的 HTML 代码):
<!-- Bootstrap -->
<link href="/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<!-- Datatables -->
<link rel="stylesheet" href="/css/dataTables.bootstrap.min.css">
<div class="table-responsive">
<table class="table table-condensed table-hover" id="customers-table">
<thead>
<tr>
<th>#</th>
<th>First name</th>
<th>Last Name</th>
<th>Email Address</th>
</tr>
</thead>
<tbody>
@foreach($customers as $customer)
<tr>
<td>{{ $customer->id }}</td>
<td>{{ $customer->first_name }}</td>
<td>{{ $customer->last_name }}</td>
<td>{{ $customer->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- Datatables -->
<script src="/js/jquery.dataTables.min.js"></script>
<script src="/js/dataTables.bootstrap.min.js"></script>
<script>
// Datatable settings
$(document).ready(function() {
$('#customers-table').DataTable({
"language": {
"lengthMenu": "Show _MENU_ entires per page",
"search": "Search:",
"decimal": ".",
"thousands": ",",
"zeroRecords": "No entries found.",
"info": "Showing entries _START_ to _END_ of total _TOTAL_",
"infoEmpty": "No entries available.",
"infoFiltered": "(filtered from _MAX_ total entries)",
"paginate": {
"first": "First",
"last": "Last",
"next": "Next",
"previous": "Previous"
}
}
});
} );
</script>
所以问题是:我需要更新什么才能添加对分页的支持?
【问题讨论】:
-
我忘记了它是如何工作的,但是 DataTables 应该支持基于服务器的方法,其中结果通过 AJAX 请求加载/分页。基本上,在 DataTables 请求之前,您不会查询
User模型,然后您可以处理限制/偏移(分页)。 -
使用laravel-datatables以服务器端处理方式处理DataTables。
标签: php laravel twitter-bootstrap datatables