Home>Article>Backend Development> yii2 implements paging and paging with search functions
This article mainly introduces the implementation of paging in yii2 and an example of the paging function with search. It has certain reference value and interested friends can refer to it.
1. Model configuration
The example will use three models. The article category table and article table can be generated with gii, and the last one is the search verification model. Among them, we only talk about the next joint table and search verification. No other operations are required.
1. Article table association
hasOne(ArticleCate::className(),['id' => 'cid']); } ?>
2. Search model
common/models/search/Create ArticleSearch.php
joinWith(['cate']);//关联文章类别表 // $query->joinWith(['author' => function($query) { $query->from(['author' => 'users']); }]); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 2, ], ]); // 从参数的数据中加载过滤条件,并验证 $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } // 增加过滤条件来调整查询对象 $query->andFilterWhere([ // 'cname' => $this->cate.cname, 'title' => $this->title, ]); $query->andFilterWhere(['like', 'title', $this->title]); //$query->andFilterWhere(['like', 'cate.cname', $this->cname]) ; return $dataProvider; } }
2. Use paging
Method 1
First, in the action of the controller, create the paging object and fill it with data:
with('cate'); $pagination = new Pagination([ 'defaultPageSize' => 3, 'totalCount' => $model->count(), ]); $model = $model->orderBy('id ASC') ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('index', [ 'model' => $model, 'pagination' => $pagination, ]); } ?>
Secondly, in the view, the template we output is the current page and Link to the page through the paging object:
$pagination, 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ]); ?>
Method 2
Controller:
with('cate'); $provider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 3, ], 'sort' => [ 'defaultOrder' => [ //'created_at' => SORT_DESC, //'title' => SORT_ASC, ] ], ]); return $this->render('index', [ 'model' => $query, 'dataProvider' => $provider ]); ?>
View:
$dataProvider, //每列都有搜索框 控制器传过来$searchModel = new ArticleSearch(); //'filterModel' => $searchModel, 'layout'=> '{items}{pager}
', 'pager'=>[ //'options'=>['class'=>'hidden']//关闭自带分页 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ], 'columns' => [ //['class' => 'yii\grid\SerialColumn'],//序列号从1开始 // 数据提供者中所含数据所定义的简单的列 // 使用的是模型的列的数据 'id', 'username', ['label'=>'文章类别', /*'attribute' => 'cid',产生一个a标签,点击可排序*/ 'value' => 'cate.cname' ], ['label'=>'发布日期','format' => ['date', 'php:Y-m-d'],'value' => 'created_at'], // 更复杂的列数据 ['label'=>'封面图','format'=>'raw','value'=>function($m){ return Html::img($m->cover,['class' => 'img-circle','width' => 30]); }], [ 'class' => 'yii\grid\DataColumn', //由于是默认类型,可以省略 'value' => function ($data) { return $data->name; // 如果是数组数据则为 $data['name'] ,例如,使用 SqlDataProvider 的情形。 }, ], [ 'class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{delete} {update}',//只需要展示删除和更新 /*'headerOptions' => ['width' => '80'],*/ 'buttons' => [ 'delete' => function($url, $model, $key){ return Html::a(' 删除', ['artdel', 'id' => $key], ['class' => 'btn btn-default btn-xs', 'data' => ['confirm' => '你确定要删除文章吗?',] ]); }, 'update' => function($url, $model, $key){ return Html::a(' 更新', ['artedit', 'id' => $key], ['class' => 'btn btn-default btn-xs']); }, ], ], ], ]); ?>
3. Search with paging function
Create search model (done before)
Control incoming data
View display control Device code:
search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } ?>
View:
['index'], 'method' => 'get', 'id' => 'cateadd-form', 'options' => ['class' => 'form-horizontal'], ]); ?> = $form->field($searchModel, 'title',[ 'options'=>['class'=>''], 'inputOptions' => ['placeholder' => '文章搜索','class' => 'input-sm form-control'], ])->label(false) ?> = Html::submitButton('Go!', ['class' => 'btn btn-sm btn-primary']) ?> = GridView::widget([ 'dataProvider' => $dataProvider, 'layout'=> '{items}{pager}
', 'pager'=>[ //'options'=>['class'=>'hidden']//关闭自带分页 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ], //这部分和上面的分页是一样的
Above That’s the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
About how to write jQuery for search paging in the YII framework
The above is the detailed content of yii2 implements paging and paging with search functions. For more information, please follow other related articles on the PHP Chinese website!