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