Yii2 paging usage and its extension methods, yii2 paging extension _ PHP Tutorial

Source: Internet
Author: User
Yii2 paging usage and its extension methods are described in detail, and yii2 paging extension is described in detail. Yii2 paging usage and its extension methods are described in detail, step by step teach you how to use the paging Yii2 page and how to expand it, yii2 page extension details

Preface:

Describe what we will talk about in this article

The use of paging, step by step to teach you how to do

Attributes that can be customized by page-based LinkPager and Pagination

How does the paging LinkPager extend to what we need?

Step 1: Let's see how to use the yii2 built-in paging class?

1. controller action

use yii\data\Pagination;$query = Article::find()->where(['status' => 1]);$countQuery = clone $query;$pages = new Pagination(['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; // The data foreach ($ models as $ model) is displayed cyclically ){//......} // display the page number echo LinkPager: widget (['pagination' => $ pages,])

Basically, the code can be completely copied and some data can be modified. I believe most people can understand it.

Next, let's take a look at the second step, which attributes can be defined by the built-in paging class?

First, let's talk about the LinkPager component.

The. pagination parameter is required. this is an instance of the Pagination class.

The default paging class is shown below

. Buttons on the top and bottom pages and 10 buttons

First, modify the buttons on the top and bottom pages to Chinese.

<? = LinkPager: widget (['pagination' => $ pages, 'nextpagelabel '=> 'next page', 'prevpagelabel' => 'previous page',]);?>

If you do not want to display the top and bottom pages, set prevPageLabel and nextPageLabel to false.

<?= LinkPager::widget([   'pagination' => $pages,   'nextPageLabel' => false,   'prevPageLabel' => false, ]); ?>

By default, neither the homepage nor the last page is displayed. you can set it as needed.

<? = LinkPager: widget (['pagination' => $ pages, 'firstpagelabel '=> 'homepage', 'lastpagelabel '=> 'Last page',]);?>

If your data is too small and there are not enough 2 pages, no page is displayed by default. if you need to, set hideOnSinglePage to false.

<?= LinkPager::widget([   'pagination' => $pages,   'hideOnSinglePage' => false, ]); ?>

The default page number is 10. you can set the number of pages that you want to display in maxButtonCount.

<?= LinkPager::widget([   'pagination' => $pages,   'maxButtonCount' => 5, ]); ?>

Some people do not like the default style. if you want to bring your own style on pages, you can set options. do not forget to implement pre, next, disabled, and other styles on your own.

<?= LinkPager::widget([   'pagination' => $pages,   'options' => ['class' => 'm-pagination'], ]); ?>

Next, let's talk about the Pagination component.

The default paging route is shown below. let's see what we can do.

/Controller/action? Page = 2 & per-page = 20

First of all, we must specify the total number of totalCount. without this parameter, paging cannot be implemented.

$pages = new Pagination([   'totalCount' => $totalCount, ]);

The default number of pages is 20. you can set pageSize for what you want

$pages = new Pagination([   'totalCount' => $totalCount,   'pageSize' => 5, ]);

From the preceding page routing, we can see that the default number is per page. if you do not want to display this parameter, set pageSizeParam to false.

$pages = new Pagination([   'totalCount' => $totalCount,   'pageSizeParam' => false, ]);

We can also see that the default page depends on the page parameter. if you want to change this parameter to p, set pageParam = p.

$pages = new Pagination([   'totalCount' => $totalCount,   'pageParam' => 'p', ]);

If your page exists on the home page, I believe you will definitely want /? P = 1 instead of/site/index? P = 1. let's see how to hide the route.

$pages = new Pagination([   'totalCount' => $totalCount,   'route' => false, ]);

You may find that there is a bug in Pagination. if we only have one page of data, but when we manually change page = 20 in the address bar, the page = 1 data will also be displayed? Of course, this is annoying in most API interfaces. However, this is not a bug, but a friendly verification. Set validatePage to false to avoid this problem.

$pages = new Pagination([   'totalCount' => $totalCount,   'validatePage' => false, ]);

Finally, let's take a new look and extend his own page! Don't look at the extensions as soon as you see them. you can get stronger and stronger only when you learn to expand them! How can it be extended? Let's Change the paging component to the one on the top and bottom pages. let's make a comparison for specific reference.

Next, let's take a look at how the effect on the right is implemented by extending the LinkPager component. The source code is shared with you. you just need to study it on your own.

<? Phpnamespace frontend \ components; use yii \ widgets \ LinkPager; use yii \ helpers \ Html; class MLinkPager extends LinkPager {public $ prevPageLabel =''; Public $ nextPageLabel =''; Public $ currentCountPageLabel =' page {currentPage}/total page {countPage} '; 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, $ currentPage <= 0, false );} // current page/count page if ($ this-> currentCountPageLabel! = False & $ pageCount) {$ currentCountPageLabel = 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 >=$ pageCount-1, false );} return Html: tag ('Nav', implode ("\ n", $ buttons), $ this-> options);} protected function renderPageButton ($ label, $ page, $ class, $ disabled, $ active) {$ options = ['class' => empty ($ Class )? $ 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 );}}

In this way, we can call MLinkPager to implement the paging effect as follows:

use frontend\components\MLinkPager; <?= MLinkPager::widget([   'pagination' => $pages, ]); ?>

Of course, there will inevitably be a lot of problems when we build our own expanded pages to teach you how to implement page extension. if you have any good comments or methods, please leave a message to me and let us communicate with each other.

I will explain what paging is used in this article. I will teach you how to create paging step by step...

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.