Home > Backend Development > PHP Tutorial > Example code for implementing yii2 paging to jump to a specific page, yii2 paging jump example_PHP tutorial

Example code for implementing yii2 paging to jump to a specific page, yii2 paging jump example_PHP tutorial

WBOY
Release: 2016-07-12 08:51:02
Original
854 people have browsed it

Yii2 paging implementation code to jump to a specific page, yii2 paging jump example

First look at the picture above to see the effect. If you feel it is wrong, please refer to how the function is implemented. !

It is not difficult to see from the picture above that the function we developed to jump to a certain page is based on the extension on linkpager. This paging extension we implemented before is obviously different. The previous one is obviously rewritten! Of course, this is not important, let's take a look at the specific implementation of GoLinkPager! The name is a bit lower, so it doesn’t matter!

1. Create a new GoLinkPager class file in the frontendcomponents directory

2. This class inherits yiiwidgetsLinkPager;, as follows:

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

3. Add the attribute public $go = false; //Whether the jump function is included. The default is false

4. Rewrite the renderPageButtons method of the parent class linkPager. For details, refer directly to the full version of the code below. You can mainly look at the code implementation of the go part.

<&#63;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 &#63; '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 &#63; $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);
 }
}
Copy after login

See the specific usage below:

<&#63;= GoLinkPager::widget([ 
 'pagination' => $pages, 
 'go' => true, 
]); &#63;>
Copy after login

It can be seen that it is extremely convenient to use! Just add an attribute go to true.

It should be noted that the go part of the html js in the complete version of the code can be modified and organized according to your own needs!

The above content is the example code of yii2 paging introduced by the editor to jump to a specific page. I hope it will be helpful to everyone!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133028.htmlTechArticleYii2 paging example code to jump to a specific page, yii2 paging jump example first see the effect above , everyone feels it is wrong, please refer to how the function is implemented! It is easy to see from the picture above that I...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template