自定义分页视图在Laravel 5
Laravel 4.2可以select在app/config/view.php
指定自定义视图,例如:
/* |-------------------------------------------------------------------------- | Pagination View |-------------------------------------------------------------------------- | | This view will be used to render the pagination link output, and can | be easily customized here to show any view you like. A clean view | compatible with Twitter's Bootstrap is given to you by default. | */ 'pagination' => 'pagination_slider-alt'
至less关于view.php
这个在Laravel 5中没有了。
有没有办法在Laravel 5中复制这种行为?
在Laravel 5.3+使用
$users->links('view.name')
在Laravel 5.0 – 5.2而不是
$users->render()
使用
@include('pagination.default', ['paginator' => $users])
视图/分页/ default.blade.php
@if ($paginator->lastPage() > 1) <ul class="pagination"> <li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}"> <a href="{{ $paginator->url(1) }}">Previous</a> </li> @for ($i = 1; $i <= $paginator->lastPage(); $i++) <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}"> <a href="{{ $paginator->url($i) }}">{{ $i }}</a> </li> @endfor <li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}"> <a href="{{ $paginator->url($paginator->currentPage()+1) }}" >Next</a> </li> </ul> @endif
而已。
如果您有很多页面,请使用以下模板:
视图/分页/ limit_links.blade.php
<?php // config $link_limit = 7; // maximum number of links (a little bit inaccurate, but will be ok for now) ?> @if ($paginator->lastPage() > 1) <ul class="pagination"> <li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}"> <a href="{{ $paginator->url(1) }}">First</a> </li> @for ($i = 1; $i <= $paginator->lastPage(); $i++) <?php $half_total_links = floor($link_limit / 2); $from = $paginator->currentPage() - $half_total_links; $to = $paginator->currentPage() + $half_total_links; if ($paginator->currentPage() < $half_total_links) { $to += $half_total_links - $paginator->currentPage(); } if ($paginator->lastPage() - $paginator->currentPage() < $half_total_links) { $from -= $half_total_links - ($paginator->lastPage() - $paginator->currentPage()) - 1; } ?> @if ($from < $i && $i < $to) <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}"> <a href="{{ $paginator->url($i) }}">{{ $i }}</a> </li> @endif @endfor <li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}"> <a href="{{ $paginator->url($paginator->lastPage()) }}">Last</a> </li> </ul> @endif
在Laravel 5中,自定义分页是基于演示者(类)而不是视图。
假设你的路由代码
$users = Users::paginate(15);
在L4中,你曾经在你的观点中做过这样的事情:
$users->appends(['sort' => 'votes'])->links();
在L5中你可以这样做:
$users->appends(['sort' => 'votes'])->render();
render()
方法接受一个Illuminate\Contracts\Pagination\Presenter
实例。 您可以创build一个实现该合约的自定义类,并将其传递给render()
方法。 请注意, Presenter
是一个接口 ,而不是一个类,因此您必须实现它,而不是扩展它。 这就是为什么你得到错误。
或者,您可以扩展Laravel paginator(以使用其分页逻辑),然后将现有分页实例( $users->...
)传递给扩展类构造函数。 这确实是我为创build基于Laravel提供的Bootstrap演示者而创build的自定义Zurb基金会演示者所做的。 它使用所有的Laravel分页逻辑,只覆盖渲染方法。
与我的自定义演示者我的看法是这样的:
with(new \Stolz\Laravel\Pagination($users->appends(['sort' => 'votes'])))->render();
我的自定义分页主持人是:
<?php namespace Stolz\Laravel; use Illuminate\Pagination\BootstrapThreePresenter; class Pagination extends BootstrapThreePresenter { /** * Convert the URL window into Zurb Foundation HTML. * * @return string */ public function render() { if( ! $this->hasPages()) return ''; return sprintf( '<ul class="pagination" aria-label="Pagination">%s %s %s</ul></div>', $this->getPreviousButton(), $this->getLinks(), $this->getNextButton() ); } /** * Get HTML wrapper for disabled text. * * @param string $text * @return string */ protected function getDisabledTextWrapper($text) { return '<li class="unavailable" aria-disabled="true"><a href="javascript:void(0)">'.$text.'</a></li>'; } /** * Get HTML wrapper for active text. * * @param string $text * @return string */ protected function getActivePageWrapper($text) { return '<li class="current"><a href="javascript:void(0)">'.$text.'</a></li>'; } /** * Get a pagination "dot" element. * * @return string */ protected function getDots() { return $this->getDisabledTextWrapper('…'); } }
而在Laravel 4.2中,我将使用:
{{ $users->links('view.name') }}
在Laravel 5中,您可以使用以下方法复制上述内容:
@include('view.name', ['object' => $users])
现在在包含视图中, $object
将有可用的分页方法,如currentPage()
, lastPage()
, lastPage()
等。
对于Laravel 5.3(也可能在其他5.X版本中)将自定义分页代码放在您的查看文件夹中。
资源/视图/分页/ default.blade.php
@if ($paginator->hasPages()) <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 --}} @foreach ($elements as $element) {{-- "Three Dots" Separator --}} @if (is_string($element)) <li class="disabled"><span>{{ $element }}</span></li> @endif {{-- Array Of Links --}} @if (is_array($element)) @foreach ($element as $page => $url) @if ($page == $paginator->currentPage()) <li class="active"><span>{{ $page }}</span></li> @else <li><a href="{{ $url }}">{{ $page }}</a></li> @endif @endforeach @endif @endforeach {{-- 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
然后从主视图文件中调用这个分页视图文件
{{ $posts->links('pagination.default') }}
不pipe你想要更新pagination / default.blade.php
也许是为时已晚,但我想分享我创build的另一个自定义分页模板,创build第一个/下一个和上一个/上一个链接。 它也隐藏了用户在第一页/最后一页时的链接。
(可选)您也可以确定链接的间隔(当前页面前后的链接数量)
用法示例:
@include('pagination', ['paginator' => $users])
要么
@include('pagination', ['paginator' => $users, 'interval' => 5])
这是要点: https : //gist.github.com/xxnoobmanxx/33f6082d009c20f77499252b89c35dea
和代码:
@if (isset($paginator) && $paginator->lastPage() > 1) <ul class="pagination"> <?php $interval = isset($interval) ? abs(intval($interval)) : 3 ; $from = $paginator->currentPage() - $interval; if($from < 1){ $from = 1; } $to = $paginator->currentPage() + $interval; if($to > $paginator->lastPage()){ $to = $paginator->lastPage(); } ?> <!-- first/previous --> @if($paginator->currentPage() > 1) <li> <a href="{{ $paginator->url(1) }}" aria-label="First"> <span aria-hidden="true">«</span> </a> </li> <li> <a href="{{ $paginator->url($paginator->currentPage() - 1) }}" aria-label="Previous"> <span aria-hidden="true">‹</span> </a> </li> @endif <!-- links --> @for($i = $from; $i <= $to; $i++) <?php $isCurrentPage = $paginator->currentPage() == $i; ?> <li class="{{ $isCurrentPage ? 'active' : '' }}"> <a href="{{ !$isCurrentPage ? $paginator->url($i) : '#' }}"> {{ $i }} </a> </li> @endfor <!-- next/last --> @if($paginator->currentPage() < $paginator->lastPage()) <li> <a href="{{ $paginator->url($paginator->currentPage() + 1) }}" aria-label="Next"> <span aria-hidden="true">›</span> </a> </li> <li> <a href="{{ $paginator->url($paginator->lastpage()) }}" aria-label="Last"> <span aria-hidden="true">»</span> </a> </li> @endif </ul> @endif
如果有人需要,Laravel 5会附带Bootstrap 4分页程序。
首先创build一个新的服务提供者。
php artisan make:provider PaginationServiceProvider
在register
方法中,将封闭传递给Laravel的paginator类,创build并返回新的主持人。
<?php namespace App\Providers; use Illuminate\Pagination\BootstrapFourPresenter; use Illuminate\Pagination\Paginator; use Illuminate\Support\ServiceProvider; class PaginationServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { Paginator::presenter(function($paginator) { return new BootstrapFourPresenter($paginator); }); } }
在config/app.php
注册新的提供者
'providers' => [ //.... App\Providers\PaginationServiceProvider::class, ]
我在Laravel的Bootstrap 4分页中find了这个例子
Laravel 5.2使用这个主持人。 您可以创build自定义演示者或使用预定义的演示者。 Laravel 5.2开箱即用地使用了BootstrapThreePrensenter
,但使用BootstrapFroutPresenter
或任何其他自定义演示程序就很容易。
public function index() { return view('pages.guestbook',['entries'=>GuestbookEntry::paginate(25)]); }
在刀片模板中,可以使用以下公式:
{!! $entries->render(new \Illuminate\Pagination\BootstrapFourPresenter($entries)) !!}
为了创build自定义的演示者,我build议观看Codecourse的video 。
Laravel 5+中Bootstrap 4分页的快速JS修复
只需在页面中放置下面的脚本:
<script> $('.pagination li').addClass('page-item'); $('.pagination li a').addClass('page-link'); $('.pagination span').addClass('page-link'); </script>
优点:节省服务器的CPU,不需要调整你的应用程序。
这里有一个Laravel 5,Bootstrap 4和没有Blade的语法(对于那些觉得无穷无尽的读者来说 )。
要使用,而不是:
{!! $users->render() !!}
使用:
@include('partials/pagination', ['paginator' => $users])
如果partials/pagination
是粘贴了下面内容的刀片模板文件。
// Number of links to show. Odd numbers work better $linkCount = 7; $pageCount = $paginator->lastPage(); if ($pageCount > 1) { $currentPage = $paginator->currentPage(); $pagesEitherWay = floor($linkCount / 2); $paginationHtml = '<ul class="pagination">'; // Previous item $previousDisabled = $currentPage == 1 ? 'disabled' : ''; $paginationHtml .= '<li class="page-item '.$previousDisabled.'"> <a class="page-link" href="'.$paginator->url($currentPage - 1).'" aria-label="Previous"> <span aria-hidden="true">«</span> <span class="sr-only">Previous</span> </a> </li>'; // Set the first and last pages $startPage = ($currentPage - $pagesEitherWay) < 1 ? 1 : $currentPage - $pagesEitherWay; $endPage = ($currentPage + $pagesEitherWay) > $pageCount ? $pageCount : ($currentPage + $pagesEitherWay); // Alter if the start is too close to the end of the list if ($startPage > $pageCount - $linkCount) { $startPage = ($pageCount - $linkCount) + 1; $endPage = $pageCount; } // Alter if the end is too close to the start of the list if ($endPage <= $linkCount) { $startPage = 1; $endPage = $linkCount < $pageCount ? $linkCount : $pageCount; } // Loop through and collect for ($i = $startPage; $i <= $endPage; $i++) { $disabledClass = $i == $currentPage ? 'disabled' : ''; $paginationHtml .= '<li class="page-item '.$disabledClass.'"> <a class="page-link" href="'.$paginator->url($i).'">'.$i.'</a> </li>'; } // Next item $nextDisabled = $currentPage == $pageCount ? 'disabled' : ''; $paginationHtml .= '<li class="page-item '.$nextDisabled.'"> <a class="page-link" href="'.$paginator->url($currentPage + 1).'" aria-label="Next"> <span aria-hidden="true">»</span> <span class="sr-only">Next</span> </a> </li>'; $paginationHtml .= '</ul>'; echo $paginationHtml; }
在@MantasD的答案旁边,我想提供全面的定制Laravel分页。 假设使用Laravel 5.2以及以下的视图:
@include('pagination.default', ['pager' => $users])
特征
- 显示Previous(上一个)和Next
- 仅在“上一页”和“下一页”页面不相同时才显示“首页”和“最后一页”图标
- 生成相对链接,例如:(10,100,500 ..等),而不是限制页面
- 使用辅助函数显示每个页面的x到y的结果。
default.blade.php
@if($pager->lastPage() != 1) <ul class="pagination"> @unless($pager->currentPage() < 3) <li class="paginate_button previous"> <a href="{{ $pager->url(1) }}" title="First Page"><i class="fa fa-angle-double-left"></i></a> </li> @endunless <li class="paginate_button previous @unless($pager->previousPageUrl())disabled @endunless"> <a href="{{ $pager->previousPageUrl() }}"><i class="fa fa-angle-left"></i></a> </li> @while($pager->paging++ < $pager->lastPage()) @if (abs($pager->paging - $pager->currentPage()) >= 2) {{-- Generate relative links (eg. +10,etc) --}} @if(in_array(abs($pager->paging - $pager->currentPage()), array(10, 50, 100, 500, 1000)) and $pager->paging != 1 and $pager->paging != $pager->lastPage()) <li class="paginate_button @unless($pager->currentPage() != $pager->paging)active @endunless"> <a title="Results from {{ PaginationStartEnd($pager->paging, $pager->perPage(), $pager->total())['start'] }} to {{ PaginationStartEnd($pager->paging, $pager->perPage(), $pager->total())['end'] }} of {{ $pager->total() }}" href="{{ $pager->url($pager->paging) }}"> <!-- + {{ $pager->paging - $pager->currentPage() }} -->{{ $pager->paging }} </a> </li> @endif @else <li class="paginate_button @unless($pager->currentPage() != $pager->paging)active @endunless"> <a title="Results from {{ PaginationStartEnd($pager->paging, $pager->perPage(), $pager->total())['start'] }} to {{ PaginationStartEnd($pager->paging, $pager->perPage(), $pager->total())['end'] }} of {{ $pager->total() }}" href="{{ $pager->url($pager->paging) }}"> {{ $pager->paging }} </a> </li> @endif @endwhile <li class="paginate_button next @unless($pager->nextPageUrl())disabled @endunless"> <a href="{{ $pager->nextPageUrl() }}"><i class="fa fa-angle-right"></i></a> </li> @unless($pager->lastPage() - $pager->currentPage() < 2) <li class="paginate_button next"> <a href="{{ $pager->url($pager->lastPage()) }}" title="Last Page"><i class="fa fa-angle-double-right"></i></a> </li> @endunless </ul> @endif
PaginationStartEnd函数
if (!function_exists('PaginationStartEnd')) { function PaginationStartEnd($currentPage, $perPage, $total) { $pageStart = number_format( $perPage * ($currentPage - 1)); $pageEnd = $pageStart + $perPage; if ($pageEnd > $total) $pageEnd = $total; $pageStart++; return array('start' => number_format($pageStart), 'end' => number_format($pageEnd)); } }
您可以根据需要使用和自定义。
注意: $ pager-> paging是variables在控制器动作中声明为0
嗨,我的代码分页:刀片@include('pagination.default',['paginator'=> $ users])
查看/分页/ default.blade.php
@if ($paginator->lastPage() > 1) si la pagina actual es distinto a 1 y hay mas de 5 hojas muestro el boton de 1era hoja --> if actual page is not equals 1, and there is more than 5 pages then I show first page button --> @if ($paginator->currentPage() != 1 && $paginator->lastPage() >= 5) << @endif <!-- si la pagina actual es distinto a 1 muestra el boton de atras --> @if($paginator->currentPage() != 1) <li> <a href="{{ $paginator->url($paginator->currentPage()-1) }}" > < </a> </li> @endif <!-- dibuja las hojas... Tomando un rango de 5 hojas, siempre que puede muestra 2 hojas hacia atras y 2 hacia adelante --> <!-- I draw the pages... I show 2 pages back and 2 pages forward --> @for($i = max($paginator->currentPage()-2, 1); $i <= min(max($paginator->currentPage()-2, 1)+4,$paginator->lastPage()); $i++) <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}"> <a href="{{ $paginator->url($i) }}">{{ $i }}</a> </li> @endfor <!-- si la pagina actual es distinto a la ultima muestra el boton de adelante --> <!-- if actual page is not equal last page then I show the forward button--> @if ($paginator->currentPage() != $paginator->lastPage()) <li> <a href="{{ $paginator->url($paginator->currentPage()+1) }}" > > </a> </li> @endif <!-- si la pagina actual es distinto a la ultima y hay mas de 5 hojas muestra el boton de ultima hoja --> <!-- if actual page is not equal last page, and there is more than 5 pages then I show last page button --> @if ($paginator->currentPage() != $paginator->lastPage() && $paginator->lastPage() >= 5) <li> <a href="{{ $paginator->url($paginator->lastPage()) }}" > >> </a> </li> @endif </ul>
感谢MantisD的post,对于Bootstrap 4,这个效果很好。
<?php $link_limit = 7; // maximum number of links (a little bit inaccurate, but will be ok for now) ?> @if ($paginator->lastPage() > 1) <div id="news_paginate" class="dataTables_paginate paging_simple_numbers"> <ul class="pagination"> <li id="news_previous" class="paginate_button page-item previous {{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}"> <a class="page-link" tabindex="0" href="{{ $paginator->url(1) }}">Previous</a> </li> @for ($i = 1; $i <= $paginator->lastPage(); $i++) <?php $half_total_links = floor($link_limit / 2); $from = $paginator->currentPage() - $half_total_links; $to = $paginator->currentPage() + $half_total_links; if ($paginator->currentPage() < $half_total_links) { $to += $half_total_links - $paginator->currentPage(); } if ($paginator->lastPage() - $paginator->currentPage() < $half_total_links) { $from -= $half_total_links - ($paginator->lastPage() - $paginator->currentPage()) - 1; } ?> @if ($from < $i && $i < $to) <li class="paginate_button page-item {{ ($paginator->currentPage() == $i) ? ' active' : '' }}"> <a class="page-link" href="{{ $paginator->url($i) }}">{{ $i }}</a> </li> @endif @endfor <li id="news_next" class="paginate_button page-item {{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}"> @if($paginator->currentPage() == $paginator->lastPage()) <a class="page-link" tabindex="0" href="{{ $paginator->url($paginator->currentPage()) }}" >End</a> @else <a class="page-link" tabindex="0" href="{{ $paginator->url($paginator->currentPage()+1) }}" >Next</a> @endif </li> </ul> </div> @endif
如果您想更改url中的页码,而不是像/ pageNo那样获取数据。 例如:/ 2。 你可以使用jQuery来更改url。 我有一些数据在获取方法与url。
$(function () { $('.pagination li a').each(function () { var link = $(this).attr('href'); var pageText = $(this).text(); var activePage = parseInt($('.pagination li.active span').text()); if (pageText.trim() == "«") { pageText = activePage - 1; } else if (pageText.trim() == "»") { pageText = activePage + 1; } link = link.replace('?', '/' + pageText + '?'); link = link.replace('&page=' + pageText, ''); $(this).attr('href', link); console.log(link); }); })
如果你想美化你的分页的外观,我使用bootstrap类来使它更简单和容易
@if ($students->lastPage() > 1) <ul class="pagination ml-auto"> <li class="{{ ($students->currentPage() == 1) ? ' disabled' : '' }} page-item"> <a class=" page-link " href="{{ $students->url(1) }}" aria-label="Previous"> <span aria-hidden="true">«</span> <span class="sr-only">Previous</span> </a> </li> @for ($i = 1; $i <= $students->lastPage(); $i++) <li class="{{ ($students->currentPage() == $i) ? ' active' : '' }} page-item"> <a class=" page-link " href="{{ $students->url($i) }}">{{ $i }}</a> </li> @endfor <li class="{{ ($students->currentPage() == $students->lastPage()) ? ' disabled' : '' }} page-item"> <a href="{{ $students->url($students->currentPage()+1) }}" class="page-link" aria-label="Next"> <span aria-hidden="true">»</span> <span class="sr-only">Next</span> </a> </li> </ul> @endif