YII2 pagination implementation jump to a specific page of the instance Code _php instance

Source: Internet
Author: User
Tags yii

First look at the effect of the picture, we feel still wrong please refer to the function how to achieve!

From the above figure is not difficult to see, we make a jump to a page of the function is based on the expansion of the Linkpager, this root of our previous implementation of the paging expansion is significantly different, before the obvious is rewritten! Of course, this is not important, we look at the specific implementation of Golinkpager! The name is a bit lower, not important!

1. New Golinkpager class file in 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 property public $go = false; Whether to include jump default false for jump function

4, rewrite the parent class Linkpager Renderpagebuttons method, the specific direct reference to the full version of the code, can mainly see the go part of the code implementation.

<?php namespace Frontend\components;
Use Yii\widgets\linkpager;
Use yii\helpers\html;
 Class Golinkpager extends Linkpager {//whether contains jump function jump default false public $go = false;
  protected function Renderpagebuttons () {$pageCount = $this->pagination->getpagecount ();
  if ($pageCount < 2 && $this->hideonsinglepage) {return ';
  } $buttons = [];
  $currentPage = $this->pagination->getpage (); $firstPageLabel = $this->firstpagelabel = = true?
  ' 1 ': $this->firstpagelabel; if ($firstPageLabel!== false) {$buttons [] = $this->renderpagebutton ($firstPageLabel, 0, $this->firstpagecssclas
  S, $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 &L
  T;= 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 &
  gt;= $pageCount-1, false); //Last page $lastPageLabel = $this->lastpagelabel = = true?
  $pageCount: $this->lastpagelabel; if ($lastPageLabel!== false) {$buttons [] = $this->renderpagebutton ($lastPageLabel, $pageCount-1, $this->LASTP
  Agecssclass, $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" > Total {$pageCount} page;/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= "bord Er:solid 1px #ccc; padding:0px; height:25px; width:46px; line-height:25px; "
   > Confirm </span> </div> gohtml;
   $buttons [] = $goHtml;
   $pageLink = $this->pagination->createurl (false); $goJs = <<<gojs $ (". Go-page"). On (' click ', function () {var _this = $ (this), _pageinput = _this.si
      Blings ("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); }
}

See below for specific use:

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

It can be seen that the use of thieves to facilitate the convenience of thieves! Add a property go to true.

Need to note that the full version of the code in the go part of the HTML JS can be tailored to their own needs to modify the collation!

The above content is a small series to introduce the YII2 paging implementation to a specific page of the example code, I hope to 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.