分页肯定是常见的数据展示输出方式。
首先控制器里面:
$model = new \backend\models\user(); $query = $model->find()->orderBy('id desc'); $pages = new \yii\data\Pagination(['totalCount' => $model->find()->count(), 'defaultPageSize' => 15]); $models = $query->offset($pages->offset) ->limit($pages->limit) ->all(); $data = [ 'models' => $models, 'pages' => $pages, ]; return $this->render('index', $data);
视图里面分页:
<div class="pagination pull-left"> <?= yii\widgets\LinkPager::widget([ 'pagination' => $pages, 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页', ]); ?> </div>
关键字词: