<?php namespace frontend\components; use yii\widgets\LinkPager; use yii\helpers\Html; class MLinkPager extends LinkPager { public $prevPageLabel = '<i></i>'; public $nextPageLabel = '<i></i>'; public $currentCountPageLabel = '第 {currentPage} 页 / 共 {countPage} 页'; public $currentCountPageClass = 'page-number'; public $hideOnSinglePage = false; public function init () { parent::init(); } public function run () { $pageCount = $this->pagination->getPageCount(); if ($pageCount < 2 && $this->hideOnSinglePage) { return ''; } $buttons = []; $currentPage = $this->pagination->getPage(); // prev page if ($this->prevPageLabel !== false) { if (($page = $currentPage - 1) < 0) { $page = 0; } $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false); } // current page / count page if ($this->currentCountPageLabel !== false && $pageCount) { $currentCountPageLabel = str_replace(['{currentPage}', '{countPage}'], [$currentPage+1, $pageCount], $this->currentCountPageLabel); $buttons[] = Html::tag('span', $currentCountPageLabel, array('class' => $this->currentCountPageClass)); } // next page if ($this->nextPageLabel !== false) { if (($page = $currentPage + 1) >= $pageCount - 1) { $page = $pageCount - 1; } $buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount - 1, false); } return Html::tag('nav', implode("\n", $buttons), $this->options); } protected function renderPageButton($label, $page, $class, $disabled, $active) { $options = ['class' => empty($class) ? $this->pageCssClass : $class]; if ($active) { Html::addCssClass($options, $this->activePageCssClass); } if ($disabled) { return false; } $linkOptions = $this->linkOptions; $linkOptions += $options; $linkOptions['data-page'] = $page; return Html::a($label, $this->pagination->createUrl($page), $linkOptions); } }
如此一来,我们调用MLinkPager实现分页效果像下面这样即可
use frontend\components\MLinkPager; <?= MLinkPager::widget([ 'pagination' => $pages, ]); ?>
当然,自己扩展的分页组建重在教大家如何去实现分页扩展,难免会有很多问题,如果你有好的意见或者方法,直接给我留言,咱们共同沟通交流。
您可能感兴趣的文章: