十年老IT知识分享 – yii2统一分页类

<!--分页start-->
<div class="pager paging">
    <?=
    \yii\widgets\LinkPager::widget([
        'pagination' => $pagination,
        //设置样式
        'options' => [
            'class' => 'pagination',
        ],
        'maxButtonCount' => 6,
        'firstPageLabel' => '首页',
        'lastPageLabel' => '末页',
        'prevPageLabel' => '上一页',
        'nextPageLabel' => '下一页',
        'hideOnSinglePage' => true,
        'disabledPageCssClass' => 'disabled',
    ]);
    ?>
    <span><?= $totalCount?></span>
</div>


<!--分页end-->

编辑统一分页类

<?php
namespace frontend\widgets;


class LinkPager extends \yii\widgets\LinkPager
{
    public $firstPageLabel = '首页';
    public $prevPageLabel = '上一页';
    public $nextPageLabel = '下一页';
    public $lastPageLabel = '末页';
    public $maxButtonCount = 6; //显示多少页
    public $options = ['class' => 'pagination pull-right']; //css样式
    public $totalTemplate = '<div class="pagination pull-right page-total-count" style="padding: 4px 10px;">总数:%s条</div>';//css样式

    public function run()
    {
        if ($this->registerLinkTags) {
            $this->registerLinkTags();
        }
        $buttons = $this->renderPageButtons();
        if($this->totalTemplate)
        {
            $buttons = sprintf($this->totalTemplate, $this->pagination->totalCount).$buttons;
        }
        echo $buttons;
    }
}

视图调用分页类

<?=
LinkPager::widget([
    'pagination' => $pagination
]);
?>

正文完