我正在尝试在数据表中添加分页。但是当我使用pagination(25)时,它仅从db中带出25行,并显示实体总数为25。没有2 | 3 | 4 |。下一步按钮转到下一个数据。它不会从数据库中获取所有数据,也不会对它们进行分页。
刀片:
<table id="example1" class="table table-bordered table-hover">
<thead>
<tr>
<th>SL</th>
<th>Date</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach($leads_list as $data){
$i++;
?>
<tr>
<td><?php echo $i;?></td>
<td>
{{$data->created_at}}
</td>
<td>
{{$data->title}}
</td>
</tr>
<?php } ?>
</tbody>
</table>
javascript:
$(function () {
$("#example1").DataTable({
"pageLength": 100,
"responsive": true,
"autoWidth": false,
});
});
控制器:
$leads_list = DB::table('leads')
->paginate(15);
return view("leads.index", compact('leads_list'));
想要从db中获取所有数据,并在每页15data的表中显示它们。 但是它仅从db中带来15行,并且没有分页。
但是如果我使用-> get(); ,它可以正常工作,但是页面变慢。
答案 0 :(得分:2)
首先,使用刀片模板无需使用<?php ?>
标签,而要回答您的问题,您必须显示分页链接:-
您的新刀片文件:-
<table id="example1" class="table table-bordered table-hover">
<thead>
<tr>
<th>SL</th>
<th>Date</th>
<th>Title</th>
</tr>
</thead>
<tbody>
@foreach ($leads_list as $list_item)
<tr>
<td>{{ $loop->index }}</td>
<td>{{ $list_item->created_at }}</td>
<td>{{ $list_item->title }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $leads_list->links() }}
答案 1 :(得分:1)
请查看文档。
https://laravel.com/docs/7.x/pagination#displaying-pagination-results。
您将必须将分页链接添加到刀片文件中。
timestamp(regexp_replace(start_time, '^(.+?)[T ](.+?)','$1 $2'))
,这将创建1,2,3等页面链接,以便您可以在分页页面之间进行切换。
如果您不使用刀片文件,则可以在视图中添加以下代码:
{{$leads_list->links()}}