温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用yii2分页功能怎么实现跳转到指定页面

发布时间:2021-02-20 16:32:19 来源:亿速云 阅读:173 作者:Leah 栏目:开发技术

本篇文章为大家展示了使用yii2分页功能怎么实现跳转到指定页面,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1、在frontend\components目录新建GoLinkPager类文件

2、该类继承yii\widgets\LinkPager;,如下:

namespace frontend\components; 
use yii\widgets\LinkPager; 
use yii\helpers\Html; 
class GoLinkPager extends LinkPager 
{ 
}

3、添加属性public $go = false; //是否包含跳转功能跳转 默认false

4、重写父类linkPager的renderPageButtons方法,具体直接参考下面完整版代码,可主要看go部分的代码实现。

<?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
    <div class="form" >
     <span class="text">共 {$pageCount} 页</span>
     <span class="text">到第</span>
     <input class="input" type="number" value="{$goPage}" min="1" max="{$pageCount}" aria-label="页码输入框" >
     <span class="text">页</span>
     <span class="btn go-page" role="button" tabindex="0" >确定</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, 
]); ?>

上述内容就是使用yii2分页功能怎么实现跳转到指定页面,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI