先说明下我们本篇文章都要讲哪些内容
第一步,我们来看看yii2自带的分页类该如何去使用?
1、controller action
<span style="color: #0000ff;">use</span><span style="color: #000000;"> yii\data\Pagination; </span><span style="color: #800080;">$query</span> = Article::find()->where(['status' => 1<span style="color: #000000;">]); </span><span style="color: #800080;">$countQuery</span> = <span style="color: #0000ff;">clone</span> <span style="color: #800080;">$query</span><span style="color: #000000;">; </span><span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span> Pagination(['totalCount' => <span style="color: #800080;">$countQuery</span>-><span style="color: #008080;">count</span><span style="color: #000000;">()]); </span><span style="color: #800080;">$models</span> = <span style="color: #800080;">$query</span>->offset(<span style="color: #800080;">$pages</span>-><span style="color: #000000;">offset) </span>->limit(<span style="color: #800080;">$pages</span>-><span style="color: #000000;">limit) </span>-><span style="color: #000000;">all(); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->render('index',<span style="color: #000000;"> [ </span>'models' => <span style="color: #800080;">$models</span>, 'pages' => <span style="color: #800080;">$pages</span>,<span style="color: #000000;"> ]);</span>
2、View
<span style="color: #0000ff;">use</span><span style="color: #000000;"> yii\widgets\LinkPager; </span><span style="color: #008000;">//</span><span style="color: #008000;">循环展示数据</span> <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$models</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$model</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> ......</span> <span style="color: #000000;">} </span><span style="color: #008000;">//</span><span style="color: #008000;">显示分页页码</span> <span style="color: #0000ff;">echo</span> LinkPager::<span style="color: #000000;">widget([ </span>'pagination' => <span style="color: #800080;">$pages</span>,<span style="color: #000000;"> ])</span>
代码基本上可以完全拷贝,修改部分数据即可,相信大多数人都是看得懂的。
我们接下来看第二步,自带的分页类都可以定义哪些属性
首先我们说说LinkPager组件
默认分页类是下面这个样子的
= LinkPager::<span style="color: #000000;">widget([ </span>'pagination' => <span style="color: #800080;">$pages</span>, 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页',<span style="color: #000000;"> ]); </span>?>
= LinkPager::<span style="color: #000000;">widget([ </span>'pagination' => <span style="color: #800080;">$pages</span>, 'nextPageLabel' => <span style="color: #0000ff;">false</span>, 'prevPageLabel' => <span style="color: #0000ff;">false</span>,<span style="color: #000000;"> ]); </span>?>
= LinkPager::<span style="color: #000000;">widget([ </span>'pagination' => <span style="color: #800080;">$pages</span>, 'firstPageLabel' => '首页', 'lastPageLabel' => '尾页',<span style="color: #000000;"> ]); </span>?>
= LinkPager::<span style="color: #000000;">widget([ </span>'pagination' => <span style="color: #800080;">$pages</span>, 'hideOnSinglePage' => <span style="color: #0000ff;">false</span>,<span style="color: #000000;"> ]); </span>?>
= LinkPager::<span style="color: #000000;">widget([ </span>'pagination' => <span style="color: #800080;">$pages</span>, 'maxButtonCount' => 5,<span style="color: #000000;"> ]); </span>?>
= LinkPager::<span style="color: #000000;">widget([ </span>'pagination' => <span style="color: #800080;">$pages</span>, 'options' => ['class' => 'm-pagination'],<span style="color: #000000;"> ]); </span>?>
接下来我们谈谈Pagination组件
默认的分页路由是下面这样子的,我们看看能做点什么
/controller/action?page=2&per-page=20
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ </span>'totalCount' => <span style="color: #800080;">$totalCount</span>,<span style="color: #000000;"> ]);</span>
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 'pageSize' => 5,<span style="color: #000000;"> ]);</span>
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 'pageSizeParam' => <span style="color: #0000ff;">false</span>,<span style="color: #000000;"> ]);</span>
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 'pageParam' => 'p',<span style="color: #000000;"> ]);</span>
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 'route' => <span style="color: #0000ff;">false</span>,<span style="color: #000000;"> ]);</span>
<span style="color: #800080;">$pages</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Pagination([ </span>'totalCount' => <span style="color: #800080;">$totalCount</span>, 'validatePage' => <span style="color: #0000ff;">false</span>, ]);
最后,我们整点新花样,扩展下他这个自带的分页!别一看见扩展俩字下面的就直接不看了,只有自己学会扩展了,以后才能越来越强!怎么个扩展法呢?我们把分页组件改为上下页那种,具体参考下图做个对比吧
[考虑目前国内网站大部分采集文章十分频繁,更有甚者不注明原文出处,原作者更希望看客们查看原文,以防有任何问题不能更新所有文章,避免误导!]