Yii2 paging usage and extension, yii2 paging use extension _ PHP Tutorial

Source: Internet
Author: User
Yii2 paging and its extension, yii2 paging extension. Yii2 pagination and its extension. yii2 pagination first describes what paging is used in this article, step by step, we will teach you how to use and expand pagination-type LinkPager and P yii2 paging, and yii2 paging-based 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.
 $ 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.
  $pages,     'nextPageLabel' => false,     'prevPageLabel' => false, ]); ?>

  • By default, neither the homepage nor the last page is displayed. you can set it as needed.
 $ 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.
  $pages,     'hideOnSinglePage' => false, ]); ?>

  • The default page number is 10. you can set the number of pages that you want to display in maxButtonCount.
  $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.
  $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

First, explain what paging is used in this article, and step by step teach you how to implement paging-type LinkPager and P...

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.