先上图看效果,大家感觉还错请参考功能怎么实现的!
从上图中不难看出,我们制定跳转到某页的功能是基于linkpager之上的扩展,这根我们之前实现的分页扩展明显不同,之前的明显就是重写了!当然,这都不重要,我们看看golinkpager的具体实现!名字起的有点lower,不重要!
1、在frontendcomponents目录新建golinkpager类文件
2、该类继承yiiwidgetslinkpager;,如下:
namespace frontendcomponents;
use yiiwidgetslinkpager;
use yiihelpershtml;
class golinkpager extends linkpager
{
}
3、添加属性public $go = false; //是否包含跳转功能跳转 默认false
4、重写父类linkpager的renderpagebuttons方法,具体直接参考下面完整版代码,可主要看go部分的代码实现。
<?php
namespace frontendcomponents;
use yiiwidgetslinkpager;
use yiihelpershtml;
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
<div class="form" style="float: left; color: #999; margin-left: 10px; font-size: 12px;">
<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>
</div>
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);
}
}
下面看具体使用:
<?= golinkpager::widget([ 'pagination' => $pages, 'go' => true, ]); ?>
可以看出,使用起来也是贼方便贼方便的!加一个属性go为true即可。
需要说明的是,完整版代码中go部分html js可根据自己需要自行修改整理!
以上内容是小编给大家介绍的yii2分页之实现跳转到具体某页的实例代码,希望对大家有所帮助!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!