Yii2 the use of paging and the extension of the method in detail, yii2 page extension detailed
Objective:
Explain what we have to say in this article
The use of pagination, step by step to teach you how to do
The pagination classes Linkpager and pagination can customize which properties
How the paging class Linkpager expanded into what we needed
In the first step, let's take a look at Yii2 's own paging class, how to use it?
1. Controller action
Use yii\data\pagination; $query = Article::find ()->where ([' Status ' = 1]); $countQuery = Clone $query; $pages = new Pa Gination ([' totalcount ' = $countQuery->count ()]); $models = $query->offset ($pages->offset) Limit ($pages->limit) ->all (); return $this->render (' index ', [ ' models ' + = $models, ' pages ' = > $pages,]);
2. View
Use yii\widgets\linkpager;//loop display data foreach ($models as $model) { //...} Show pagination page number echo linkpager::widget ([ ' pagination ' = $pages,])
The code can basically be completely copied, modify part of the data can be, I believe most people are able to understand.
Let's look at the second step, and the page class that comes with can define which properties
First, let's talk about Linkpager components.
The. Pagination parameter is required, this is an example of our pagination class
The default paging class looks like this
. Up and Down buttons and 10 buttons
First, we change the buttons of the upper and lower pages into Chinese
<?= linkpager::widget ([ ' pagination ' = ' $pages, ' nextpagelabel ' = ' next ', ' prevpagelabel ' = ' Previous page ',]);?>
If you do not want to display the upper and lower pages, you can set Prevpagelabel and Nextpagelabel to False
<?= linkpager::widget ([ ' pagination ' = $pages, ' nextpagelabel ' = False, ' prevpagelabel ' = False,]);?>
Default does not show home page also last, if you need, can set this
<?= linkpager::widget ([ ' pagination ' = $pages, ' firstpagelabel ' = ' home ', ' lastpagelabel ' + = ' last ',]);?>
If your data is too small, not enough 2 pages, the default does not show paging, if you need to set Hideonsinglepage=false can
<?= linkpager::widget ([ ' pagination ' = $pages, ' hideonsinglepage ' = false,]);?>
By default, the page number is 10 pages and you can set Maxbuttoncount to the pages you want to display.
<?= linkpager::widget ([ ' pagination ' = $pages, ' maxbuttoncount ' = 5,]);?>
Some people do not like the default style, want to page with their own style, you can set options, do not forget to implement the style of pre,next,disabled, etc.
<?= linkpager::widget ([ ' pagination ' + = $pages, ' options ' = [' class ' = ' m-pagination '],]);?>
Next we talk about pagination components
The default paging route is the following, so let's see what we can do.
/controller/action?page=2&per-page=20
First of all, we have to specify the total number of totalcount, without this parameter, paging is not a way to achieve
$pages = new Pagination ([ ' totalcount ' = $totalCount,]);
The number of default paging is 20, you can set pagesize as you want
$pages = new Pagination ([ ' totalcount ' = = $totalCount, ' pageSize ' = 5,]);
From the above paging we can see that the default with the number of pages per page per-page if you do not want to show this parameter, set pagesizeparam=false just fine
$pages = new Pagination ([ ' totalcount ' = = $totalCount, ' pagesizeparam ' = false,]);
We can also see that the default page depends on the parameter page, if you want to change the parameter to P, set pageparam=p just fine
$pages = new Pagination ([ ' totalcount ' = = $totalCount, ' pageparam ' = ' P ',]);
If your pagination exists on the home page, I'm sure you want/?p=1 instead of/site/index?p=1, let's see how to hide the route
$pages = new Pagination ([ ' totalcount ' = = $totalCount, ' route ' = = False,]);
Perhaps you will find that the page class pagination has a bug, if we have only 1 pages of data, but manually change the address bar page=20, will also display page=1 data? Of course, this is annoying in most interface APIs. However, this is not a bug, but a friendly validation. Set Validatepage=false to avoid this problem
$pages = new Pagination ([ ' totalcount ' = = $totalCount, ' validatepage ' = false,]);
Finally, we have a twist on the whole point, extending his own page! Do not see the expansion of the two words under the directly do not see, only their own learning to expand, the future will be more and more strong! How about an extension? Let's change the paging component to the next page, the specific reference to make a comparison
Let's take a look at how the right side effect is implemented by extending the Linkpager component. Source sharing to everyone, like to take their own research can.
<?phpnamespace frontend\components;use yii\widgets\linkpager;use Yii\helpers\html;class MLinkPager extends linkpager{Public $prevPageLabel = ''; Public $nextPageLabel = ''; Public $currentCountPageLabel = ' {currentpage} page/Total {countpage} page '; Public $currentCountPageClass = ' page-number '; Public $hideOnSinglePage = false; Public Function init () {parent::init (); Public Function Run () {$pageCount = $this->pagination->getpagecount (); if ($pageCount < 2 && $this->hideonsinglepage) {return '; } $buttons = []; $currentPage = $this->pagination->getpage (); Prev Page if ($this->prevpagelabel!== false) {if ($page = $currentPage-1) < 0) {$page = 0; } $buttons [] = $this->renderpagebutton ($this->prevpagelabel, $page, $this->prevpagecssclass, $currentPag E <= 0, false); }//Current Page/count page if ($this->currentcountpagelabel!== false && $pageCount) {$currentC Ountpagelabel = Str_replace ([' {currentpage} ', ' {countpage} '], [$currentPage +1, $pageCount], $this- Currentcountpagelabel); $buttons [] = Html::tag (' span ', $currentCountPageLabel, Array (' class ' = $this->currentcountpageclass)); }//Next page if ($this->nextpagelabel!== false) {if ($page = $currentPage + 1) >= $pageCount-1) { $page = $pageCount-1; } $buttons [] = $this->renderpagebutton ($this->nextpagelabel, $page, $this->nextpagecssclass, $currentPage &G t;= $pageCount-1, false); } return Html::tag (' Nav ', implode ("\ n", $buttons), $this->options); } protected function Renderpagebutton ($label, $page, $class, $disabled, $active) {$options = [' class ' = * = Empty ($c Lass)? $this->pagecssclass: $class]; if ($active) {Html::addcssclass ($options, $this->activepagecssclass); } if ($disabled) {return false; } $linkOptions = $this->linkoptions; $linkOptions + = $options; $linkOptions [' data-page '] = $page; Return Html::a ($label, $this->pagination->createurl ($page), $linkOptions); }}
As a result, we call Mlinkpager to achieve a paging effect like this
Use Frontend\components\mlinkpager; <?= mlinkpager::widget ([ ' pagination ' = $pages,]);?>
Of course, their own expansion of the page set to teach you how to achieve paging extension, there will inevitably be many problems, if you have good ideas or methods, directly to my message, we communicate together.
http://www.bkjia.com/PHPjc/1133107.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133107.html techarticle Yii2 The use of paging and its extension method, yii2 the details of the extension of the introduction: The description of what we have to say in this article on what the use of pagination, step by step to teach you how to do the pagination ...