我是 Laravel 的新手,我正在尝试使用 Yajra Datatable 具有服务器端功能的插件。该插件适用于少量记录,但我的记录量很大,大约 100000 条记录。
为了加快控制器中的处理速度,我使用 take(10) 限制查询结果,并使用另一个查询来计算总结果。到目前为止一切都很好。
问题是如何管理研究。除了主要研究领域之外,我还使用了单独列搜索,但我不知道如何返回正确的记录数来使用单独搜索过滤器管理分页。
我认为个人搜索键位于
$columns = $request->get('columns');
中,但我不知道如何管理计数的查询。
谢谢您的宝贵建议。
HTML查看代码:
<table id="oTable">
<thead>
<tr>
<th>Action</th>
<th>Brand</th>
<th>Code</th>
<th>Description</th>
</tr>
<tr>
<th class="no_search"></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
</table>
Jquery 代码:
$('#oTable').DataTable({
dom: 'lfrtip',
"processing": true,
"serverSide": true,
"ajax": '{!! url('getRecords') !!}',
"columns": [
{data: 'items.id', name: 'items_id'},
{data: 'brands.description', name: 'brands_description'},
{data: 'items.code', name: 'items_code'},
{data: 'items.description', name: 'items_description'}
],
columnDefs: [
{targets: 'no_sort', orderable: false}
],
initComplete: function () {
this.api().columns().every(function () {
var column = this;
var columnClass = column.header().className;
if (columnClass.indexOf('no_search') != false) {
var input = document.createElement("input");
$(input).addClass('form-control');
$(input).appendTo($(column.header()).empty())
.on('change', function () {
column.search($(this).val(), false, false, true).draw();
});
}
});
}
});
控制器方法:
public function getRecords(Request $request) {
$search = $request->input('search.value');
$columns = $request->get('columns');
$count_total = \DB::table('items')
->join('brands', 'item.brand', '=', 'brands.code')
->count();
$count_filter = \DB::table('items')
->join('brands', 'items.brand', '=', 'brands.code')
->where( 'brands.description' , 'LIKE' , '%'.$search.'%')
->orWhere( 'items.description' , 'LIKE' , '%'.$search.'%')
->orWhere( 'items.code' , 'LIKE' , '%'.$search.'%')
->count();
$items= \DB::table('items')
->join('brands', 'items.brand', '=', 'brands.code')
->select(
'items.id as items_id',
'items.code as items_code',
'items.description as items_description',
'brands.description as brands_description'
) -> take(10);
return Datatables::of($items)
->with([
"recordsTotal" => $count_total,
"recordsFiltered" => $count_filter,
])
->rawColumns(['items_id','brands_description'])
->make(true);
}
您只需更换控制器内部的方法并按如下所述设置内容即可。 它将解决您的问题
通过启用分页提高性能
public function getRecords(Request $request) {
$search = $request->input('search.value');
$columns = $request->get('columns');
$pageSize = ($request->length) ? $request->length : 10;
$itemQuery = \DB::table('items')
->join('brands', 'items.brand', '=', 'brands.code');
// $itemQuery->orderBy('items_id', 'asc');
$itemCounter = $itemQuery->get();
$count_total = $itemCounter->count();
$count_filter = 0;
if($search != ''){
$itemQuery->where( 'brands.description' , 'LIKE' , '%'.$search.'%')
->orWhere( 'items.description' , 'LIKE' , '%'.$search.'%')
->orWhere( 'items.code' , 'LIKE' , '%'.$search.'%')
$count_filter = $itemQuery->count();
}
$itemQuery->select(
'items.id as items_id',
'items.code as items_code',
'items.description as items_description',
'brands.description as brands_description'
);
$start = ($request->start) ? $request->start : 0;
$itemQuery->skip($start)->take($pageSize);
$items = $itemQuery->get();
if($count_filter == 0){
$count_filter = $count_total;
}
return Datatables::of($items)
->with([
"recordsTotal" => $count_total,
"recordsFiltered" => $count_filter,
])
->rawColumns(['items_id','brands_description'])
->make(true);
}
public function getRecords(Request $request) {
//Use this way of your code
$search = $request->input('search.value');
$columns = $request->get('columns');
$order = isset($_GET[ 'order' ]) ? $_GET[ 'order' ] : [];
$count_total = \DB::table('items')
->join('brands', 'item.brand', '=', 'brands.code')
->count();
$count_filter = \DB::table('items')
->join('brands', 'items.brand', '=', 'brands.code')
->where('brands.description', 'LIKE', '%' . $search . '%')
->orWhere('items.description', 'LIKE', '%' . $search . '%')
->orWhere('items.code', 'LIKE', '%' . $search . '%')
->count();
$items = \DB::table('items')
->join('brands', 'items.brand', '=', 'brands.code')
->select(
'items.id as items_id',
'items.code as items_code',
'items.description as items_description',
'brands.description as brands_description'
);
foreach ($order as $o) {
if(isset($columns[ $o[ 'column' ] ])) {
$items = $items->orderBy($columns[ $o[ 'column' ] ][ 'name' ], $o[ 'dir' ]);
}
}
$items = $items->take(10);
return Datatables::of($items)
->with([
"recordsTotal" => $count_total,
"recordsFiltered" => $count_filter,
])
->rawColumns(['items_id', 'brands_description'])
->make(TRUE);
}
将实现用作服务,请查看此处的文档 https://datatables.yajrabox.com/services/basic
补充说明,
有时分页的页面可能无法正确显示数据(data not shown),所以你可以添加“draw”和“data”,一一尝试。 (此解决方案适用于旧版本的 Yajra 数据表)
$draw = $request->get('draw');
...
return Datatables::of($items)
->with([
"draw" => (int)$draw,
"recordsTotal" => $count_total,
"recordsFiltered" => $count_filter,
"data" => $items
])
->rawColumns(['items_id','brands_description'])
->make(true);
使用skipPaging()进行服务器端分页
官方链接:https://yajrabox.com/docs/laravel-datatables/master/skip-paging