Laravel 中的数据表分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44586737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
datatable pagination in laravel
提问by Peter
I am using laravel 5.0 I am also using datatable jquery plugin to display grid.
我正在使用 laravel 5.0 我也在使用数据表 jquery 插件来显示网格。
Controller mehtod
控制器方法
public function index() {
$jobs = \App\Job::orderBy('created_at', 'DESC')->limit(1000)->get();
return View::make('jobs.index', ['jobs' => $jobs]);
}
The issue: Right now I hard-coded the ->limit(1000) to 1000 jobs in datatable grid to display it but i have more then 1000 records to display.
问题:现在我在数据表网格中将 ->limit(1000) 硬编码为 1000 个作业以显示它,但我有超过 1000 条记录要显示。
What I want? I want to display 500 records with grid and then 500 records. I am not sure if there is any call back data-table plugin function available? I need a dynamic way to load next 500
我想要的是?我想用网格显示 500 条记录,然后显示 500 条记录。不知道有没有回调数据表插件功能?我需要一种动态方式来加载下一个 500
NOTE: I am not willing to us this solution of scrolling https://datatables.net/extensions/scroller/examples/initialisation/server-side_processing.html
注意:我不愿意向我们提供这种滚动解决方案 https://datatables.net/extensions/scroller/examples/initialisation/server-side_processing.html
回答by Hasan Tareque
You can user ajax data source:
您可以使用 ajax 数据源:
please visit : https://datatables.net/examples/ajax/objects.html
请访问:https: //datatables.net/examples/ajax/objects.html
Example PHP Script:
PHP 脚本示例:
// function will process the ajax request
public function getMembers(Request $request) {
$draw = $request->get('draw');
$start = $request->get('start');
$length = $request->get('length');
$search = (isset($filter['value']))? $filter['value'] : false;
$total_members = 1000; // get your total no of data;
$members = $this->methodToGetMembers($start, $length); //supply start and length of the table data
$data = array(
'draw' => $draw,
'recordsTotal' => $total_members,
'recordsFiltered' => $total_members,
'data' => $members,
);
echo json_encode($data);
}
Example JavaScript :
示例 JavaScript:
$('#all-member-table').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
url: base_url+"ajax/members"
},
"columns": [
{ data: '1' },
{ data: '2' },
{ data: '3' },
{ data: '4' },
{ data: '5' },
]
} );
Example HTML:
示例 HTML:
<table id="all-member-table">
<thead>
<tr>
<th>Column1</th>
<th>Column2</th>
<th>Column3</th>
<th>Column4</th>
<th>Column5</th>
</tr>
</thead>
</table>
回答by Tuncay Elvana?a?
I think above answer should be extended with search feature.
我认为上面的答案应该通过搜索功能进行扩展。
Update the answer;
更新答案;
$filter = $request->get('search');
$search = (isset($filter['value']))? $filter['value'] : false;
where('somecolumnonyourdb','like', '%'.$search.'%')
This works for me
这对我有用