Yii2 paging and its extension, yii2 paging Extension

Source: Internet
Author: User

Yii2 paging and its extension, yii2 paging Extension

First, let's explain 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

  • Top and bottom buttons 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.

[Considering that most of the articles collected on Chinese websites are very frequent at present, the author does not specify the source of the original article. The original author prefers the readers to check the original article to avoid updating all the articles due to any problems and avoid misleading.]

Continue reading

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.