yii2分页扩展实现页码跳转
首先展示最终效果截图,若觉得实用可继续了解其实现原理。

从效果图中可以看出,该跳转功能是在原有linkpager基础上的扩展,这与之前直接重写分页逻辑的方式有所不同。尽管实现路径不同,重点在于解析GoLinkPager的具体代码结构。命名风格虽不严谨,但不影响功能演示。
第一步,在frontend\components目录下创建GoLinkPager类的源文件。
第二步,让该类继承自yii\widgets\LinkPager基类,具体结构参考后续代码块。
namespace frontend\components;
use yii\widgets\LinkPager;
use yii\helpers\Html;
class GoLinkPager extends LinkPager
{
}
第三步,在类中定义公开属性public $go = false;,用于标识是否启用页面跳转功能,默认值为false。
第四步,重写父类LinkPager中的renderPageButtons方法。详细实现请参照下方完整代码,重点关注涉及跳转功能的逻辑部分。
<?php
namespace frontend\components;
use yii\widgets\LinkPager;
use yii\helpers\Html;
class GoLinkPager extends LinkPager
{
// 是否包含跳转功能跳转 默认false
public $go = false;
protected function renderPageButtons()
{
$pageCount = $this->pagination->getPageCount();
if ($pageCount < 2 && $this->hideOnSinglePage) {
return '';
}
$buttons = [];
$currentPage = $this->pagination->getPage();
// first page
$firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel;
if ($firstPageLabel !== false) {
$buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
}
// prev page
if ($this->prevPageLabel !== false) {
if (($page = $currentPage - 1) < 0) {
$page = 0;
}
$buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false);
}
// internal pages
list($beginPage, $endPage) = $this->getPageRange();
for ($i = $beginPage; $i <= $endPage; ++$i) {
$buttons[] = $this->renderPageButton($i + 1, $i, null, false, $i == $currentPage);
}
// 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);
}
// last page
$lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel;
if ($lastPageLabel !== false) {
$buttons[] = $this->renderPageButton($lastPageLabel, $pageCount - 1, $this->lastPageCssClass, $currentPage >= $pageCount - 1, false);
}
// go
if ($this->go) {
$goPage = $currentPage + 2;
$goHtml = <<<goHtml
<span class="text">共 {$pageCount} 页</span>
<span class="text">到第</span>
<input class="input" type="number" value="{$goPage}" min="1" max="{$pageCount}" aria-label="页码输入框" style="text-align: center; height: 25px; line-height: 20px; margin-top: 5px; width: 46px;">
<span class="text">页</span>
<span class="btn go-page" role="button" tabindex="0" style="border: solid 1px #ccc; padding: 0px; height: 25px; width: 46px; line-height: 25px;">确定</span>
goHtml;
$buttons[] = $goHtml;
$pageLink = $this->pagination->createUrl(false);
$goJs = <<<goJs
$(".go-page").on("click", function () {
var _this = $(this),
_pageInput = _this.siblings("input"),
goPage = _pageInput.val(),
pageLink = "{$pageLink}";
pageLink = pageLink.replace("page=1", "page="+goPage);
if (goPage >= 1 && goPage <= {$pageCount}) {
window.location.href=pageLink;
} else {
_pageInput.focus();
}
});
goJs;
$this->view->registerJs($goJs);
}
return Html::tag('ul', implode("\n", $buttons), $this->options);
}
}</code></pre>
接下来展示具体的调用方式:
<pre class="brush:php;"><code class="language-php">
<?= GoLinkPager::widget([
'pagination' => $pages,
'go' => true,
]); ?>
使用过程非常简便,只需将对应的分页组件配置项中的go属性设置为true即可激活跳转功能。
需要注意的是,完整代码中负责生成跳转框的HTML结构和JavaScript脚本,开发者可根据实际需求进行调整或优化。
以上即为Yii2中实现分页跳转到指定页码的完整实例代码,希望能对读者有所帮助。