


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

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
