yii2實(shí)現(xiàn)分頁(yè),帶搜索的分頁(yè)功能示例
一、模型配置
事例會(huì)用到三個(gè)models。文章類別表和文章表用gii生成下即可,最后一個(gè)是搜索驗(yàn)證模型。其中,只講下一個(gè)聯(lián)表和搜索驗(yàn)證。其他不用操作。
1.文章表關(guān)聯(lián)
<?php //...other code //關(guān)聯(lián) public function getCate(){ return $this->hasOne(ArticleCate::className(),['id' => 'cid']); } ?>
2.搜索模型
common/models/search/創(chuàng)建ArticleSearch.php
<?php namespace common\models\search; use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; use common\models\Article; class ArticleSearch extends Article { //public $cname;//文章類別名 /** * @inheritdoc */ public function rules() { return [ [['cid','created_at', 'updated_at'], 'integer'], [['id', 'desc','title','cover','content'], 'safe'], ]; } /** * @inheritdoc */ public function scenarios() { // bypass scenarios() implementation in the parent class return Model::scenarios(); } //搜索 public function search($params) { $query = Article::find(); // $query->joinWith(['cate']);//關(guān)聯(lián)文章類別表 // $query->joinWith(['author' => function($query) { $query->from(['author' => 'users']); }]); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 2, ], ]); // 從參數(shù)的數(shù)據(jù)中加載過(guò)濾條件,并驗(yàn)證 $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; } // 增加過(guò)濾條件來(lái)調(diào)整查詢對(duì)象 $query->andFilterWhere([ // 'cname' => $this->cate.cname, 'title' => $this->title, ]); $query->andFilterWhere(['like', 'title', $this->title]); //$query->andFilterWhere(['like', 'cate.cname', $this->cname]) ; return $dataProvider; } }
二、分頁(yè)使用
方式一
首先在控制器的動(dòng)作中,創(chuàng)建分頁(yè)對(duì)象并且為其填充數(shù)據(jù):
<?php //other code use yii\data\Pagination; public function actionArticlelist() { //分頁(yè)讀取類別數(shù)據(jù) $model = Article::find()->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, ]); } ?>
其次在視圖中我們輸出的模板為當(dāng)前頁(yè)并通過(guò)分頁(yè)對(duì)象鏈接到該頁(yè):
<?php use yii\widgets\LinkPager; use yii\helpers\Html; use yii\helpers\Url; //other code foreach ($models as $model) { // 在這里顯示 $model } // 顯示分頁(yè) echo LinkPager::widget([ 'pagination' => $pagination, 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ]); ?>
方式二
控制器:
<?php $query = Article::find()->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 ]); ?>
視圖:
<?php use yii\grid\GridView; echo GridView::widget([ 'dataProvider' => $dataProvider, //每列都有搜索框 控制器傳過(guò)來(lái)$searchModel = new ArticleSearch(); //'filterModel' => $searchModel, 'layout'=> '{items}<div class="text-right tooltip-demo">{pager}</div>', 'pager'=>[ //'options'=>['class'=>'hidden']//關(guān)閉自帶分頁(yè) 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ], 'columns' => [ //['class' => 'yii\grid\SerialColumn'],//序列號(hào)從1開(kāi)始 // 數(shù)據(jù)提供者中所含數(shù)據(jù)所定義的簡(jiǎn)單的列 // 使用的是模型的列的數(shù)據(jù) 'id', 'username', ['label'=>'文章類別', /*'attribute' => 'cid',產(chǎn)生一個(gè)a標(biāo)簽,點(diǎn)擊可排序*/ 'value' => 'cate.cname' ], ['label'=>'發(fā)布日期','format' => ['date', 'php:Y-m-d'],'value' => 'created_at'], // 更復(fù)雜的列數(shù)據(jù) ['label'=>'封面圖','format'=>'raw','value'=>function($m){ return Html::img($m->cover,['class' => 'img-circle','width' => 30]); }], [ 'class' => 'yii\grid\DataColumn', //由于是默認(rèn)類型,可以省略 'value' => function ($data) { return $data->name; // 如果是數(shù)組數(shù)據(jù)則為 $data['name'] ,例如,使用 SqlDataProvider 的情形。 }, ], [ 'class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{delete} {update}',//只需要展示刪除和更新 /*'headerOptions' => ['width' => '80'],*/ 'buttons' => [ 'delete' => function($url, $model, $key){ return Html::a('<i class="glyphicon glyphicon-trash"></i> 刪除', ['artdel', 'id' => $key], ['class' => 'btn btn-default btn-xs', 'data' => ['confirm' => '你確定要?jiǎng)h除文章嗎?',] ]); }, 'update' => function($url, $model, $key){ return Html::a('<i class="fa fa-file"></i> 更新', ['artedit', 'id' => $key], ['class' => 'btn btn-default btn-xs']); }, ], ], ], ]); ?>
三、搜索帶分頁(yè)功能
- 創(chuàng)建搜索模型(前面己做)
- 控制傳入數(shù)據(jù)
- 視圖顯示控制器代碼:
<?php public function actionIndex() { $searchModel = new ArticleSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } ?>
視圖:
<?php $form = ActiveForm::begin([ 'action' => ['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']) ?> <?php ActiveForm::end(); ?> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'layout'=> '{items}<div class="text-right tooltip-demo">{pager}</div>', 'pager'=>[ //'options'=>['class'=>'hidden']//關(guān)閉自帶分頁(yè) 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ], //這部分和上面的分頁(yè)是一樣的
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- yii分頁(yè)組件用法實(shí)例分析
- Yii2.0框架實(shí)現(xiàn)帶分頁(yè)的多條件搜索功能示例
- Yii2.0小部件GridView(兩表聯(lián)查/搜索/分頁(yè))功能的實(shí)現(xiàn)代碼
- yii2分頁(yè)之實(shí)現(xiàn)跳轉(zhuǎn)到具體某頁(yè)的實(shí)例代碼
- Yii2分頁(yè)的使用及其擴(kuò)展方法詳解
- Yii分頁(yè)用法實(shí)例詳解
- Yii使用CLinkPager分頁(yè)實(shí)例詳解
- Yii列表定義與使用分頁(yè)方法小結(jié)(3種方法)
- 詳解Yii實(shí)現(xiàn)分頁(yè)的兩種方法
- Yii1.1中通過(guò)Sql查詢進(jìn)行的分頁(yè)操作方法
- yii使用bootstrap分頁(yè)樣式的實(shí)例
- YII2框架中分頁(yè)組件的使用方法示例
相關(guān)文章
PHP flush()與ob_flush()的區(qū)別詳解
本篇文章是對(duì)PHP中的flush函數(shù)與ob_flush函數(shù)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06VB中的RasEnumConnections函數(shù)返回632錯(cuò)誤解決方法
這篇文章主要介紹了VB中的RasEnumConnections函數(shù)返回632錯(cuò)誤解決方法,使用MSDN中的例子在XP SP3系統(tǒng)上出現(xiàn)的錯(cuò)誤,需要的朋友可以參考下2014-07-07PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】
這篇文章主要介紹了PHP設(shè)計(jì)模式:原型模式Prototype,結(jié)合實(shí)例形式詳細(xì)分析了PHP原型模式Prototype的基本概念、功能、原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05ThinkPHP實(shí)現(xiàn)微信支付(jsapi支付)流程教程詳解
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)微信支付(jsapi支付)流程教程詳解,需要的朋友可以參考下2018-03-03destoon之URL Rewrite(偽靜態(tài))設(shè)置方法詳解
這篇文章主要介紹了destoon的URL Rewrite(偽靜態(tài))設(shè)置方法,需要的朋友可以參考下2014-06-06ThinkPHP連接數(shù)據(jù)庫(kù)及主從數(shù)據(jù)庫(kù)的設(shè)置教程
這篇文章主要介紹了ThinkPHP連接數(shù)據(jù)庫(kù)及主從數(shù)據(jù)庫(kù)的設(shè)置方法,是進(jìn)行大型web項(xiàng)目開(kāi)發(fā)十分有用的技巧,需要的朋友可以參考下2014-08-08Yii2中hasOne、hasMany及多對(duì)多關(guān)聯(lián)查詢的用法詳解
hasOne、hasMany是Yii2特有的用于多表關(guān)聯(lián)查詢的函數(shù),平時(shí)在使用多表關(guān)聯(lián)查詢的時(shí)候建議使用它們。這篇文章主要介紹了Yii2中hasOne、hasMany及多對(duì)多關(guān)聯(lián)查詢的用法詳解,需要的朋友可以參考下2017-02-02php 利用array_slice函數(shù)獲取隨機(jī)數(shù)組或前幾條數(shù)據(jù)
這篇文章主要介紹了php 利用array_slice函數(shù)獲取隨機(jī)數(shù)組或前幾條數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2015-09-09php檢查函數(shù)必傳參數(shù)是否存在的實(shí)例詳解
這篇文章主要介紹了php檢查函數(shù)必傳參數(shù)是否存在的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08