网站首页 PHP laravel自定义分页样式
在使用laravel分页时候,默认的分页样式不是自己想要的,需要自己修改。
默认样式是 < 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 98 99 >
这个样式太丑了,而且我不想要这么多的 页码,只想实现如下的页码
< 1 2 3 4 5 6 7 > < 6 7 8 9 10 11 > < 11 12 13 14 15 16 17 18 19 20 >
php原生代码
<?php
//分页显示页面数量,取值范围 3-n
$paging_number = 7;
if($paging_number<3){
$paging_number = 3;
}
//当前页
$paging_current_page = $_GET['p'];
//共几页
$paging_last_page = 15;
if(($paging_number%2) == 0){
if($paging_last_page <= $paging_number){
$paging_start = 1;
$paging_end = $paging_last_page;
}else if($paging_current_page < ($paging_number/2+1)){
$paging_start = 1;
$paging_end = $paging_number;
}else if($paging_current_page >= ($paging_number/2+1) && (($paging_current_page + ($paging_number/2 - 1)) <= $paging_last_page)){
$paging_start = $paging_current_page - ($paging_number/2);
$paging_end = $paging_current_page + ($paging_number/2 - 1);
}else{
$paging_start = $paging_last_page - $paging_number + 1;
$paging_end = $paging_last_page;
}
}else{
if($paging_last_page <= $paging_number){
$paging_start = 1;
$paging_end = $paging_last_page;
}else if($paging_current_page < ceil($paging_number/2)){
$paging_start = 1;
$paging_end = $paging_number;
}else if($paging_current_page >= ceil($paging_number/2) && (($paging_current_page + floor($paging_number/2)) <= $paging_last_page)){
$paging_start = $paging_current_page - floor($paging_number/2);
$paging_end = $paging_current_page + floor($paging_number/2);
}else{
$paging_start = $paging_last_page - ($paging_number - 1);
$paging_end = $paging_last_page;
}
}
for($i=$paging_start;$i<=$paging_end;$i++){
if($i != $paging_current_page){
echo "<span><a href='test.php?p=".$i."'>".$i."</a></span> ";
}else{
echo "<span style='color:red;'><a href='test.php?p=".$i."'><b>$i</b></a></span> ";
}
}
laravel分页模板代码
@if ($paginator->hasPages())
@php
//指定显示的页码数量,取值范围3-n
$paging_number = 10;
if($paging_number<3){
$paging_number = 3;
}
//当前页
$paging_current_page = $paginator->currentPage();
//共几页
$paging_last_page = $paginator->lastPage();
if(($paging_number%2) == 0){
if($paging_last_page <= $paging_number){
$paging_start = 1;
$paging_end = $paging_last_page;
}else if($paging_current_page < ($paging_number/2+1)){
$paging_start = 1;
$paging_end = $paging_number;
}else if($paging_current_page >= ($paging_number/2+1) && (($paging_current_page + ($paging_number/2 - 1)) <= $paging_last_page)){
$paging_start = $paging_current_page - ($paging_number/2);
$paging_end = $paging_current_page + ($paging_number/2 - 1);
}else{
$paging_start = $paging_last_page - $paging_number + 1;
$paging_end = $paging_last_page;
}
}else{
if($paging_last_page <= $paging_number){
$paging_start = 1;
$paging_end = $paging_last_page;
}else if($paging_current_page < ceil($paging_number/2)){
$paging_start = 1;
$paging_end = $paging_number;
}else if($paging_current_page >= ceil($paging_number/2) && (($paging_current_page + floor($paging_number/2)) <= $paging_last_page)){
$paging_start = $paging_current_page - floor($paging_number/2);
$paging_end = $paging_current_page + floor($paging_number/2);
}else{
$paging_start = $paging_last_page - ($paging_number - 1);
$paging_end = $paging_last_page;
}
}
@endphp
<ul class="pagination">
{{-- Previous Page Link --}}
@if ($paginator->onFirstPage())
<li class="disabled"><span>«</span></li>
@else
<li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">«</a></li>
@endif
{{-- Pagination Elements --}}
@for ($i = $paging_start; $i <= $paging_end; $i++)
@if ($i == $paginator->currentPage())
<li class="active"><span>{{ $i }}</span></li>
@else
<li><a href="{{ $paginator->url($i) }}">{{ $i }}</a></li>
@endif
@endfor
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
<li><a href="{{ $paginator->nextPageUrl() }}" rel="next">»</a></li>
@else
<li class="disabled"><span>»</span></li>
@endif
</ul>
@endif
原文:https://blog.csdn.net/u011897510/article/details/77823390
将criterion.blade.php模板放到模板目录,其他模板页面渲染语法
{{ $data->links('criterion') }}
转载请注明出处!