先说明下我们本篇文章都要讲哪些内容
第一步,我们来看看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>
追梦A系列(11.0版本,以下11.0均简称为A)是针对企业网站定制设计的,模板采用全新AS3.0代码编辑,拥有更快的运行和加载速度,A系列模板主要针对图片展示,拥有简洁大气展示效果,并且可以自由扩展图片分类,同时还拥有三个独立页面介绍栏目,一个新闻栏目,一个服务介绍栏目,一个幻灯片展示和flv视频播放栏目。A系列模板对一些加载效果进行了修改,包括背景的拉伸模式以及标题的展示方式等都进行了调整,同
0
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>, ]);
最后,我们整点新花样,扩展下他这个自带的分页!别一看见扩展俩字下面的就直接不看了,只有自己学会扩展了,以后才能越来越强!怎么个扩展法呢?我们把分页组件改为上下页那种,具体参考下图做个对比吧
[考虑目前国内网站大部分采集文章十分频繁,更有甚者不注明原文出处,原作者更希望看客们查看原文,以防有任何问题不能更新所有文章,避免误导!]
继续阅读
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号