Home >Backend Development >PHP Tutorial >The use of yii2 paging and its extension, the use of yii2 paging extension_PHP tutorial
First explain what we are going to talk about in this article
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 default paging class looks like this
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页',<span> ]); </span>?>
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'nextPageLabel' => <span>false</span>, 'prevPageLabel' => <span>false</span>,<span> ]); </span>?>
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'firstPageLabel' => '首页', 'lastPageLabel' => '尾页',<span> ]); </span>?>
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'hideOnSinglePage' => <span>false</span>,<span> ]); </span>?>
<?= LinkPager::<span>widget([ </span>'pagination' => <span>$pages</span>, 'maxButtonCount' => 5,<span> ]); </span>?>
<?= 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
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>,<span> ]);</span>
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'pageSize' => 5,<span> ]);</span>
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'pageSizeParam' => <span>false</span>,<span> ]);</span>
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'pageParam' => 'p',<span> ]);</span>
<span>$pages</span> = <span>new</span><span> Pagination([ </span>'totalCount' => <span>$totalCount</span>, 'route' => <span>false</span>,<span> ]);</span>
<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