1、控制器:IndexController.php
1 <?php 2 //引入分页类 3 use yii\data\Pagination; 4 use app\models\Index; 5 6 public function actionidnex(){ 7 $data = Index::find()->andWhere([‘id‘ => ‘10‘]); 8 $pages = new Pagination([‘totalCount‘ =>$data->count(), ‘pageSize‘ => ‘2‘]); 9 $model = $data->offset($pages->offset)->limit($pages->limit)->all(); 10 11 return $this->render(‘index‘,[ 12 ‘model‘ => $model, 13 ‘pages‘ => $pages, 14 ]); 15 } 16 ?>
好的,到这里,控制器部分基本就结束了。
2、我们接续看 view 里面的代码:idnex.php
1 <?php 2 //引入linkPager 3 use yii\widgets\LinkPager; 4 ?> 5 6 foreach($model as $key=>$val) 7 { 8 //这里就是遍历数据了,省略...... 9 } 10 <!--显示页码--> 11 <?= LinkPager::widget([‘pagination‘ => $pages]); ?>
时间: 2024-10-23 01:33:02