Implementation of yii2 paging to jump to the instance code of a specific page, yii2 paging to jump to the instance

Source: Internet
Author: User

Implementation of yii2 paging to jump to the instance code of a specific page, yii2 paging to jump to the instance

Let's look at the results first. If you still feel wrong, please refer to how to implement the function!

It is not hard to see that the function for redirecting to a page is based on the extension above linkpager. The paging extension we previously implemented is obviously different, and 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, not important!

1. Create a GoLinkPager class file in the frontend \ components directory.

2. This class inherits yii \ widgets \ LinkPager;, as follows:

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

3. Add public $ go = false; // whether to include the jump function. The default value is false.

4. Rewrite the renderPageButtons method of the parent linkPager class. For details, refer to the full code below. You can refer to the code implementation in the go section.

<? Phpnamespace frontend \ components; use yii \ widgets \ LinkPager; use yii \ helpers \ Html; class GoLinkPager extends LinkPager {// whether the jump function is included; default value: 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} pages </span> <span class =" text "> to </span> <input class =" input "type =" number "value =" {$ goPage} "min =" 1 "max =" {$ pageCount} "aria-label =" page number input box "style =" text- align: center; height: 25px; line-height: 20px; margin-top: 5px; width: 46px; "> <span class =" text "> page </span> <span class =" btn go-page "role =" button "tabindex =" 0 "style =" border: solid 1px # ccc; padding: 0px; height: 25px; width: 46px; line-height: 25px; "> OK </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 );}}

The specific usage is as follows:

<?= GoLinkPager::widget([  'pagination' => $pages,  'go' => true, ]); ?>

We can see that it is convenient for thieves to use it! Add an attribute go to true.

In the full version of the Code, go html js can be modified as needed!

The above content is the example code for yii2 paging to jump to a specific page. I hope it will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.