Limit amount of links shown with Laravel pagination(限制 Laravel 分页显示的链接数量)
问题描述
有什么简单的方法可以限制 Laravel 分页显示的链接数量?
Any easy way to limit how many links are shown with Laravels pagination?
目前最多显示 13 个链接(上一页,1 2 3 4 5 7 8 .. 78 79 下一页)
Currently it shows 13 links at most (Prev, 1 2 3 4 5 7 8 .. 78 79 next)
然而,这对于移动设备来说太多了,变成了两行导航......有没有办法将链接设置为例如只显示 10 个?
This however is too much for mobile devices and becomes a two line navigation... is there any way to set the links to e.g. only show 10?
我搞乱了分页演示者,但实际上似乎没有任何效果.
I have messed around with the pagination presenter but nothing actually seemed to work.
谢谢
推荐答案
定义自定义演示者的旧方法不适用于 Laravel 5.3+,显示的链接数量似乎是在 中硬编码的
参数:Illuminate/Pagination/UrlWindow::make()
的 $onEachSide
The old way of defining a custom presenter doesn't work with Laravel 5.3+, the number of links shown seems to be hard-coded in the $onEachSide
parameter of Illuminate/Pagination/UrlWindow::make()
:
public static function make(PaginatorContract $paginator, $onEachSide = 3)
我最终只是编写了自己的 render() 函数,从 LengthAwarePaginator
I ended up just writing my own render() function, stealing some code from LengthAwarePaginator
/**
* Stole come code from LengthAwarePaginator::render() and ::elements() to allow for a smaller UrlWindow
*
* @param LengthAwarePaginator $paginator
* @param int $onEachSide
* @return string
*/
public static function render(LengthAwarePaginator $paginator, $onEachSide = 2)
{
$window = UrlWindow::make($paginator, $onEachSide);
$elements = array_filter([
$window['first'],
is_array($window['slider']) ? '...' : null,
$window['slider'],
is_array($window['last']) ? '...' : null,
$window['last'],
]);
return LengthAwarePaginator::viewFactory()->make(LengthAwarePaginator::$defaultView, [
'paginator' => $paginator,
'elements' => $elements,
])->render();
}
}
我们使用 Twig,所以我将其注册为 Twig 过滤器,我想可以为 Blade 做类似的事情.
We use Twig, so I registered this as a Twig filter, I imagine something similar could be done for Blade.
这篇关于限制 Laravel 分页显示的链接数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:限制 Laravel 分页显示的链接数量
基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01