yii的CURD快速创建增删改查,默认是没有分页的,只需要简单的配置就可以实现完美的分页效果
废话不多说,直接上代码;
//比如我的文章搜索模型postsearch.php只需要添加 */ public function search($params) { $query = PostsModel::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider([ 'query' => $query, //只需要添加这一句 'pagination' => [ 'pageSize' => 5, ], ]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere([ 'id' => $this->id, 'cat_id' => $this->cat_id, 'user_id' => $this->user_id, 'is_valid' => $this->is_valid, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]); $query->andFilterWhere(['like', 'title', $this->title]) ->andFilterWhere(['like', 'summary', $this->summary]) ->andFilterWhere(['like', 'content', $this->content]) ->andFilterWhere(['like', 'label_img', $this->label_img]) ->andFilterWhere(['like', 'user_name', $this->user_name]); return $dataProvider; } } //上面的代码就可以实现简单的分页效果如下图
上面的分页效果是有点丑;在稍微修改一下在你的view里面
//只需要加入下面的属性到你的 <?= GridView::widget([ 里面即可 'pager'=>[ 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页', 'maxButtonCount' => 5, 'lastPageLabel' => '尾页', ],