先说明下我们本篇文章都要讲哪些内容
第一步,我们来看看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>
代码基本上可以完全拷贝,修改部分数据即可,相信大多数人都是看得懂的。
我们接下来看第二步,自带的分页类都可以定义哪些属性
首先我们说说LinkPager组件
默认分页类是下面这个样子的
<?= 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>?>
接下来我们谈谈Pagination组件
默认的分页路由是下面这样子的,我们看看能做点什么
/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>, ]);
最后,我们整点新花样,扩展下他这个自带的分页!别一看见扩展俩字下面的就直接不看了,只有自己学会扩展了,以后才能越来越强!怎么个扩展法呢?我们把分页组件改为上下页那种,具体参考下图做个对比吧
[考虑目前国内网站大部分采集文章十分频繁,更有甚者不注明原文出处,原作者更希望看客们查看原文,以防有任何问题不能更新所有文章,避免误导!]
继续阅读