YII2.0自带分页
来源:互联网 发布:个人信息数据买卖qq群 编辑:程序博客网 时间:2024/06/06 12:43
普通分页1.建立模型参考前几篇的内容。2.控制器层 1)先引用分页类Paginationuse app\models\Position; use yii\data\Pagination; 2) 写自己的方法: public function actionPositionList(){ //分页及列表展示 $query = Position::find(); //使用模型,变量名随意,但要注意和后面对应 $page = new Pagination(['totalCount' => $query->count(),'pageSize'=>'3']); $models = $query->offset($page->offset) ->limit($page->limit) ->all(); return $this->render('position-list', [ 'models' => $models, 'pages' => $page, ]); }3.视图层(Yii是混编模式)<?php use yii\helpers\Html; use yii\widgets\LinkPager;?>……其他代码<? foreach($models as $k=>$v):?>……其他代码<? endForeach; ?>……其他代码<?= LinkPager::widget(['pagination' => $pages]) ?> //pagination是内置属性,不能改//firstPageLabel是首页//lastPageLabel是最后一页//prevPageLabel是上一页//nextPageLabel是下一页//maxButtonCount是显示个数