yii2-GridView在開(kāi)發(fā)中常用的功能及技巧
數(shù)據(jù)網(wǎng)格或者說(shuō) GridView 小部件是Yii中最強(qiáng)大的部件之一。
它有一個(gè)屬性名叫 dataProvider ,這個(gè)屬性能夠提供一個(gè)數(shù)據(jù)提供者的示例并且可以顯示所提供的數(shù)據(jù),即使用 yii\grid\GridView::columns 屬性的一組列配置,在一個(gè)表格中渲染每一行數(shù)據(jù)。
例如,
use yii\grid\GridView; echo yii\grid\GridView::widget([ 'dataProvider' => $dataProvider, ]);一、表格列
表格的列是通過(guò) GridView 配置項(xiàng)中的 yii\grid\GridView::columns 屬性配置的.
<?php use yii\grid\GridView; echo GridView::widget([ 'dataProvider' => $dataProvider, //表格列值搜索功能,注意一定要配合attribute才會(huì)顯示 //$searchModel = new ArticleSearch(); 'filterModel' => $searchModel, //重新定義分頁(yè)樣式 '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自增長(zhǎng) // 數(shù)據(jù)提供者中所含數(shù)據(jù)所定義的簡(jiǎn)單的列 // 使用的是模型的列的數(shù)據(jù) 'id', 'username', // 更復(fù)雜的列數(shù)據(jù) [ 'class' => 'yii\grid\DataColumn', //由于是默認(rèn)類(lèi)型,可以省略 'value' => function ($data) { return $data->name; // 如果是數(shù)組數(shù)據(jù)則為 $data['name'] , 例如,使用 SqlDataProvider 的情形。 }, ], ['label'=>'標(biāo)題','value' => 'title'], ['label'=>'文章內(nèi)容','format' => 'html','value' => 'content'], [ 'label'=>'文章類(lèi)別', /*'attribute' => 'cid',產(chǎn)生一個(gè)a標(biāo)簽,點(diǎn)擊可排序*/ 'value' => 'cate.cname' //關(guān)聯(lián)表 ], [ //動(dòng)作列yii\grid\ActionColumn //用于顯示一些動(dòng)作按鈕,如每一行的更新、刪除操作。 'class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{delete} {update}',//只需要展示刪除和更新 'headerOptions' => ['width' => '240'], 'buttons' => [ 'delete' => function($url, $model, $key){ return Html::a('<i class="fa fa-ban"></i> 刪除', ['del', 'id' => $key], [ 'class' => 'btn btn-default btn-xs', 'data' => ['confirm' => '你確定要?jiǎng)h除文章嗎?',] ] ); }, ], ], ], ]); ?>1. 處理時(shí)間
數(shù)據(jù)列的主要配置項(xiàng)是 yii\grid\DataColumn::format 屬性。
它的值默認(rèn)是使用 \yii\i18n\Formatter 應(yīng)用組件。
[ 'label'=>'更新日期', 'format' => ['date', 'php:Y-m-d'], 'value' => 'updated_at' ], //or [ //'attribute' => 'created_at', 'label'=>'更新時(shí)間', 'value'=>function($model){ return date('Y-m-d H:i:s',$model->created_at); }, 'headerOptions' => ['width' => '170'], ],2. 處理圖片
[ 'label'=>'封面圖', 'format'=>'raw', 'value'=>function($m){ return Html::img($m->cover, ['class' => 'img-circle', 'width' => 30] ); } ],
[ 'attribute' => 'title', 'value' => function ($model, $key, $index, $column) { return Html::a($model->title, ['article/view', 'id' => $key]); }, 'format' => 'raw', ], 4. 數(shù)據(jù)列顯示枚舉值(男/女) [ 'attribute' => 'sex', 'value'=>function ($model,$key,$index,$column){ return $model->sex==1?'男':'女'; }, //在搜索條件(過(guò)濾條件)中使用下拉框來(lái)搜索 'filter' => ['1'=>'男','0'=>'女'], //or 'filter' => Html::activeDropDownList($searchModel, 'sex',['1'=>'男','0'=>'女'], ['prompt'=>'全部'] ) ], [ 'label'=>'產(chǎn)品狀態(tài)', 'attribute' => 'pro_name', 'value' => function ($model) { $state = [ '0' => '未發(fā)貨', '1' => '已發(fā)貨', '9' => '退貨,已處理', ]; return $state[$model->pro_name]; }, 'headerOptions' => ['width' => '120'] ]
掃碼二維碼 獲取免費(fèi)視頻學(xué)習(xí)資料
- 本文固定鏈接: http://www.wangchenghua.com/post/6798/
- 轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載必須在正文中標(biāo)注并保留原文鏈接
- 掃碼: 掃上方二維碼獲取免費(fèi)視頻資料
查 看2022高級(jí)編程視頻教程免費(fèi)獲取