Yii2.0小部件GridView(兩表聯(lián)查/搜索/分頁)功能的實(shí)現(xiàn)代碼
GidView兩表聯(lián)查/搜索/分頁當(dāng)我們?cè)谝粋€(gè)網(wǎng)格視圖中顯示活動(dòng)數(shù)據(jù)的時(shí)候,你可能會(huì)遇到這種情況,就是顯示關(guān)聯(lián)表的列的值,為了使關(guān)聯(lián)列能夠排序,你需要連接關(guān)系表,以及添加排序規(guī)則到數(shù)據(jù)提供者的排序組件中,對(duì)數(shù)據(jù)進(jìn)行搜索......
以下是【金聰采編】分享的內(nèi)容全文:
以下是【金聰采編】分享的內(nèi)容全文:
GridView 兩表聯(lián)查/搜索/分頁
當(dāng)我們?cè)谝粋€(gè)網(wǎng)格視圖中顯示活動(dòng)數(shù)據(jù)的時(shí)候,你可能會(huì)遇到這種情況,就是顯示關(guān)聯(lián)表的列的值,為了使關(guān)聯(lián)列能夠排序,你需要連接關(guān)系表,以及添加排序規(guī)則到數(shù)據(jù)提供者的排序組件中,對(duì)數(shù)據(jù)進(jìn)行搜索,排序。
Ⅰ.控制器層Controller
<?php namespace backend/controllers;header("Content-type:text/html;charset=utf-8");use Yii;use yii/web/Controller; //超級(jí)控制器類use backend/models/BooksInfo; //表Model類use backend/models/InfoSearch; //引入搜索Model類use yii/data/ActiveDataProvider; //小部件數(shù)據(jù)源類use yii/grid/GridView; //查詢小部件/** *@abstract BooksController *@author NING <[email ning@163.com]> *@version [version 1.0] [書籍管理] */class BooksInfoController extends Controller{ //書籍列表 public function actionIndex() { $searchModel = new InfoSearch(); //實(shí)例化searchModel[搜索Model] if(!empty($_GET['InfoSearch'])){ $getSearch = Yii::$app->request->get(); //接收搜索字段 $data = $searchModel->search($getSearch); }else{ //小部件查詢數(shù)據(jù) $data = new ActiveDataProvider([ 'query' => BooksInfo::find(), //查詢數(shù)據(jù) 'pagination' => [ 'pageSize' => 2, //每頁顯示條數(shù) ], 'sort' => [ 'defaultOrder' => [ // 'created_at' => SORT_DESC, 'id' => SORT_ASC, //[字段]設(shè)置排序? ] ], ]); } //傳送查詢數(shù)據(jù)、搜素Model return $this->render('index',['data'=>$data,'searchModel'=>$searchModel]); }?>Ⅱ.查詢模型層Model
<?php namespace backend/models;use Yii;use yii/db/ActiveRecord;/** *@abstract [BookForm] *@author NING <[email ning@163.com]> *@version [vector 1.0] [書籍詳情模型] */class BooksInfo extends ActiveRecord{ /** * @設(shè)置表名 */ public static function tableName() { return '{{%books_info}}'; } //關(guān)聯(lián)表 public function getBooksType(){ // hasOne要求返回兩個(gè)參數(shù) 第一個(gè)參數(shù)是關(guān)聯(lián)表的類名 第二個(gè)參數(shù)是兩張表的關(guān)聯(lián)關(guān)系 // 這里id是books_type表的id, 關(guān)聯(lián)books_info表的type_id return $this->hasOne(BooksType::className(), ['id' => 'type_id']); } public function attributeLabels() { return [ 'id' => 'ID', 'book_name' => '書籍名稱', 'book_face' => '書籍封面', 'type_id' => '書籍分類ID', 'type_name' => '書籍分類', ]; }}?>Ⅲ.搜索模型層Search
<?phpnamespace backend/models; //命名空間use Yii;use yii/base/Model; //引入基類Modeluse yii/data/ActiveDataProvider; //引入數(shù)據(jù)源類/** *@abstract [搜索Model] *@return [type] *@author NING <[email ning@163.com]> */// 注意:此處繼承的是查詢Model--->BooksInfoclass InfoSearch extends BooksInfo{ public $type_name; //定義屬性變量 // 只有在 rules() 函數(shù)中聲明的字段才可以搜索 public function rules() { return [ // [['book_name','type_name'], 'safe'], [['type_name'], 'safe'], ]; } public function scenarios() { // 旁路在父類中實(shí)現(xiàn)的 scenarios() 函數(shù) return Model::scenarios(); } public function search($params) { $query = BooksInfo::find(); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 1, ], ]); /*這里的articlecategory是article模型里面關(guān)聯(lián)的方法名,除了首字母,其他都要完全一樣,否則會(huì)報(bào)錯(cuò)*/ $query->joinWith(['booksType']); // 從參數(shù)的數(shù)據(jù)中加載過濾條件,并驗(yàn)證 if (!($this->load($params) && $this->validate())) { return $dataProvider; } // 增加過濾條件來調(diào)整查詢對(duì)象 $query->andFilterWhere(['like', 'book_name', $this->book_name]); //添加關(guān)聯(lián)字段過濾條件[注意:此處books_type.type_name中books_type為分類表名] $query->andFilterWhere(['like', 'books_type.type_name', $this->type_name]); return $dataProvider; }}?>Ⅳ.視圖層View
<?php use yii/grid/GridView;use yii/data/ActiveDataProvider;use yii/grid/ActionColumn;use yii/helpers/Html;$this->title = '圖書列表';?><!-- 面包屑 --><ol class="breadcrumb"> <li><a href="#" rel="external nofollow" rel="external nofollow" >Home</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" >圖書信息</a></li> <li class="active">圖書列表</li></ol><?phpecho GridView::widget([ 'dataProvider' => $data, //數(shù)據(jù)源 'filterModel' => $searchModel, //搜索列 'columns' => [ // ['filterModel' => $searchModel], ['class' => 'yii/grid/CheckboxColumn'], //復(fù)選框列 ['attribute' => 'id'], ['attribute' => 'book_name',], ['attribute' => 'book_face','content'=>function($model){ // 圖片顯示 return Html::img($model->book_face,['width'=>'50']); }], [ 'attribute' => 'type_name', 'value' => 'booksType.type_name', //兩表聯(lián)查[書籍類型] ], ['class' => 'yii/grid/ActionColumn','header'=>'操作'], //動(dòng)作列 ], 'pager' => [//自定義分頁樣式以及顯示內(nèi)容 'prevPageLabel'=>'上一頁', 'nextPageLabel'=>'下一頁', 'firstPageLabel' => '第一頁', 'lastPageLabel' => '最后一頁', 'options'=>['style'=>'margin-left:200px;','class'=>"pagination"], ],]);?>Ⅴ.效果展示
總結(jié)
以上所述是小編給大家介紹的Yii2.0小部件GridView(兩表聯(lián)查/搜索/分頁)功能的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)金聰精品網(wǎng)站的支持!

侵權(quán)舉報(bào)/版權(quán)申訴




