有没有简单的方法来限制使用Laravels分页显示多少链接?

Currently it shows 13 links at most (Prev, 1 2 3 4 5 7 8 .. 78 79 next)

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?

我把分页演示者搞得一团糟,但实际上似乎什么都不管用.

Thanks

推荐答案

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)

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过滤,我想刀锋也可以做类似的事情.

Laravel相关问答推荐

发送邮箱后,Laravel重定向偶尔会导致错误500

Laravel中的策略对我不起作用,这是我的代码

LaravelEloquent 的模型如何将值从控制器内部的函数传递到Render()

运行Docker时,安装PHP8.1时返回错误

如何在 laravel 5.8 中显示自定义消息?

Laravel Eloquent 查找日期超过 2 天的行

如何在 NetBeans 中添加带有点 (blade.php) 的自定义文件扩展名?

Laravel 4:验证前修剪输入的最佳实践

Laravel 合集日期比较

Laravel 5.5 在迁移文件中设置整数字段的大小

Laravel - 如何恢复本地存储符号链接并刷新

Laravel 4 定义 RESTful 控制器

在 Laravel 中显示已注册的路由

如果值存在于另一个字段数组中,Laravel 验证规则

Laravel 4 上传图片表单

Select,where JSON 包含数组

Laravel 接口

如何在 laravel 用户删除中重置自动增量?

Laravel 部署……有标准的方式吗?

Laravel 更新查询