


The use of yii2 paging and its extension, the use of yii2 paging extension_PHP tutorial
The use and extension of yii2 paging, the use and extension of yii2 paging
First explain what we are going to talk about in this article
- How to use pagination, teach you step by step
- What attributes can be customized for both the paging classes LinkPager and Pagination
- How to extend the paging class LinkPager to what we need
In the first step, let’s take a look at how to use the paging class that comes with yii2?
1. controller action
<span>use</span><span> yii\data\Pagination; </span><span>$query</span> = Article::find()->where(['status' => 1<span>]); </span><span>$countQuery</span> = <span>clone</span> <span>$query</span><span>; </span><span>$pages</span> = <span>new</span> Pagination(['totalCount' => <span>$countQuery</span>-><span>count</span><span>()]); </span><span>$models</span> = <span>$query</span>->offset(<span>$pages</span>-><span>offset) </span>->limit(<span>$pages</span>-><span>limit) </span>-><span>all(); </span><span>return</span> <span>$this</span>->render('index',<span> [ </span>'models' => <span>$models</span>, 'pages' => <span>$pages</span>,<span> ]);</span>
2. View
<span>use</span><span> yii\widgets\LinkPager; </span><span>//</span><span>循环展示数据</span> <span>foreach</span> (<span>$models</span> <span>as</span> <span>$model</span><span>) { </span><span>//</span><span> ......</span> <span>} </span><span>//</span><span>显示分页页码</span> <span>echo</span> LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>,<span> ])</span>
The code can basically be completely copied and some data can be modified. I believe most people can understand it.
Let’s look at the second step. What attributes can be defined in the built-in paging class
First let’s talk about the LinkPager component
- The pagination parameter is required, this is an instance of our Pagination class
The default paging class looks like this
- Up and down page buttons and 10 buttons
- First, we change the buttons for the previous and next pages to Chinese
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页',<span> ]); </span>?>
- If you don’t want to display the previous and next pages, you can set prevPageLabel and nextPageLabel to false
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'nextPageLabel' => <span>false</span>, 'prevPageLabel' => <span>false</span>,<span> ]); </span>?>
- The home page and last page are not displayed by default. If you need, you can set it like this
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'firstPageLabel' => '首页', 'lastPageLabel' => '尾页',<span> ]); </span>?>
- If your data is too small, not enough for 2 pages, paging will not be displayed by default. If you need it, just set hideOnSinglePage=false
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'hideOnSinglePage' => <span>false</span>,<span> ]); </span>?>
- The default displayed page number is 10 pages, you can set maxButtonCount to the number of pages you want to display
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'maxButtonCount' => 5,<span> ]); </span>?>
- Some people don’t like the default style and want to have their own style for pagination. You can set options. Don’t forget to implement pre, next, disabled and other styles by yourself
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'options' => ['class' => 'm-pagination'],<span> ]); </span>?>
Next let’s talk about the Pagination component
The default paging route is as follows, let’s see what we can do
/controller/action?page=2&per-page=20
- First of all, we must specify the total number of items totalCount. Without this parameter, paging cannot be achieved
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>,<span> ]);</span>
- The default number of pages is 20, you can set pageSize to what you want
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'pageSize' => 5,<span> ]);</span>
- We can see from the paging route above that the default number per page is per-page. If you don’t want to display this parameter, just set pageSizeParam=false
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'pageSizeParam' => <span>false</span>,<span> ]);</span>
- We can also see that the default page depends on the parameter page. If you want to change the parameter to p, just set pageParam=p
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'pageParam' => 'p',<span> ]);</span>
- If your pagination exists on the homepage, I believe you definitely want /?p=1 instead of /site/index?p=1. Let’s see how to hide the route
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'route' => <span>false</span>,<span> ]);</span>
- Maybe you will find a bug in the paging class Pagination. Suppose we only have 1 page of data, but when we manually change page=20 in the address bar, will the data of page=1 also be displayed? Of course, this is annoying in most interface APIs. However, this is not a bug, but a friendly verification. Set validatePage=false to avoid this problem
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'validatePage' => <span>false</span>, ]);
Finally, let’s add a new twist and expand its built-in paging! Don’t just stop reading as soon as you see the word “expansion”. Only when you learn to expand can you become stronger and stronger in the future! What kind of expansion method? Let’s change the paging component to a top and bottom page. Please refer to the picture below for comparison
[Considering that most domestic websites currently collect articles very frequently, and some even do not indicate the source of the original article, the original author hopes that readers can check the original article to prevent any problems and not update all articles to avoid misleading! ]
Continue reading

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
