如何修改 GridView 小部件中的 Yii2 分页以显示第一页和最后一页?

How modify Yii2 pagination in GridView widget to show first and last pages?

分页的默认视图是“1, 2, 3, ..., 10”

我需要将其转换为“(第一页),x,x,x,(当前页),x,x,x,(最后一页)”

我该怎么做?

您需要指定 $firstPageLabel and $lastPageLabel properties of LinkPager(它们默认为 false,这意味着这些链接不会显示)。您可以像这样将它传递给 GridView

<?= GridView::widget([
    ...
    'pager' => [
        'firstPageLabel' => 'First',
        'lastPageLabel' => 'Last',
    ],
    ...
]) ?>

对于样式,还有两个附加属性:$firstPageCssClass and $lastPageCssClass

请注意,您可以将此单独应用于 LinkPager,而无需使用 GridView

<?= GridView::widget([
    'pager' => [
        'firstPageLabel' => 'First',
        'lastPageLabel'  => 'Last'
    ],
    ...
]) ?>

注意自 Yii2 版本 2.0.11 起,您可以使用 config/main.php 文件中的容器定义为整个应用程序设置 firstPageLabellastPageLabel 默认值:

$config = [

    ....

    'container' => [
        'definitions' => [
            'yii\widgets\LinkPager' => [
                'firstPageLabel' => 'First',
                'lastPageLabel'  => 'Last'
            ]
        ]
    ]
];

http://www.yiiframework.com/doc-2.0/guide-concept-configurations.html#application-configurations

了解更多

如果您正在使用 Bootstrap 4.x.x

'container' => [
   'definitions' => [
        \yii\widgets\LinkPager::class => \yii\bootstrap4\LinkPager::class,
        'yii\bootstrap4\LinkPager' => [
            'firstPageLabel' => 'First',
            'lastPageLabel'  => 'Last'
      ]
   ],
],