基于数据提供者
public function actionIndex() { $page = Yii::$app->get('page', 0); $modelClass = $this->modelClass; return new ActiveDataProvider( [ 'query' => $modelClass::find()->asArray(), 'pagination' => ['pageSize' =>10, 'page' => $page], ] ); }
基于分页类
public function actionIndex() { $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, ]); }
基于分页类实现接口
//分页 $new_pageSize = Yii::$app->request->get('limits'); $new_page = Yii::$app->request->get('page'); $query = Train::find()->where(['status' => 1])->orderBy("id desc"); $pageSize = $new_pageSize ? $new_pageSize : 10; $page = $new_page - 1 ? $new_page - 1 : 0; $pagination = new Pagination([ 'defaultPageSize' => $pageSize, 'totalCount' => $query->count(), 'page' => $page, ]); $data =$query->offset($pagination->offset)->limit($pagination->limit)->asArray()->all();