Laravel分页显示的链接数量限制 [英] Limit amount of links shown with Laravel pagination

查看:326
本文介绍了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+,显示的链接数似乎是硬编码在$onEachSideIlluminate/Pagination/UrlWindow::make()的参数:

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分页显示的链接数量限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆