欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析Yii2中GridView常見操作

 更新時(shí)間:2016年04月22日 11:55:37   作者:白狼  
本文是小編給大家收集整理些有關(guān)網(wǎng)絡(luò)上Yii2中GridView常見操作中出現(xiàn)的大部分問題,本文做一個(gè)總結(jié)特此分享到腳本之家平臺供大家參考

本文是小編給大家收集整理些有關(guān)網(wǎng)絡(luò)上GridView出現(xiàn)的大部分問題,本文做一個(gè)總結(jié)特此分享到腳本之家平臺供大家參考。

如果下面有沒說到的GridView常見問題,下方留言,我會進(jìn)行補(bǔ)充。

下拉搜索

日期格式化并實(shí)現(xiàn)日期可搜索

根據(jù)參數(shù)進(jìn)行是否顯示

鏈接可點(diǎn)擊跳轉(zhuǎn)

顯示圖片

html渲染

自定義按鈕

設(shè)定寬度等樣式

自定義字段

自定義行樣式

增加按鈕調(diào)用js操作

yii2 GridView 下拉搜索實(shí)現(xiàn)案例教程

yii2 GridView 日期格式化并實(shí)現(xiàn)日期可搜索 案例

是否顯示某列案例

我們舉一個(gè)簡單的案例

條件:有一個(gè)get形參數(shù)type

需求:僅且type的值等于1的時(shí)候,列name才顯示,否則該列不顯示

代碼實(shí)現(xiàn)如下:

[
'attribute' => 'name',
'value' => $model->name,
'visible' => intval(Yii::$app->request->get('type')) == 1,
],

實(shí)現(xiàn)方式也是很簡單滴。

鏈接可點(diǎn)擊跳轉(zhuǎn)案例

這個(gè)跟接下來我們要說的html渲染的效果十分類似,這里要說的是列的屬性值 format,具體都有哪些格式可查看文件 yii\i18n\Formatter.php,各種format都可以解決

[
'attribute' => 'order_id',
'value' => function ($model) {
return Html::a($model->order_id, "/order?id={$model->order_id}", ['target' => '_blank']);
},
'format' => 'raw',
],

顯示圖片案例

同上,這里只需要指定format格式為image即可,format第二個(gè)參數(shù)可設(shè)定圖片大小,可參考下面的代碼

[
'label' => '頭像',
'format' => [
'image', 
[
'width'=>'84',
'height'=>'84'
]
],
'value' => function ($model) { 
return $model->image; 
}
],

html渲染案例

什么意思喃,舉個(gè)例子,我們有一個(gè)字段,標(biāo)記為title,但是這個(gè)title不一樣,ta含有html標(biāo)簽,我們不想在頁面上展示<p>title123<p>這種形式,我們想要title123以p標(biāo)簽的形式展示,代碼可參考如下,只需要指定format為raw形式即可

[
'attribute' => 'title',
'value' => function ($model) { 
return Html::encode($model->title); 
},
'format' => 'raw',
],

自定義按鈕案例

往往列表頁我們不想要?jiǎng)h除按鈕,想在增加一個(gè)比如獲取xxx按鈕,怎么搞呢?這里需要設(shè)置ActionColumn類,修改配置項(xiàng)template并在buttons項(xiàng)增加template里增加的get-xxx即可

[
'class' => 'yii\grid\ActionColumn',
'template' => '{get-xxx} {view} {update}',
'header' => '操作',
'buttons' => [
'get-xxx' => function ($url, $model, $key) { 
return Html::a('獲取xxx', $url, ['title' => '獲取xxx'] ); 
},
],
],

設(shè)定寬度案例

舉個(gè)簡單的例子,我們的title列,太長了,能不能給我先定下這一列的寬度?

答案:能。

請看示例:

[
'attribute' => 'title',
'value' => 'title',
'headerOptions' => ['width' => '100'],
],

只需要指定配置項(xiàng)headerOptions即可。

自定義字段案例

啥時(shí)自定義?這里我們是指在表格里增加一列且數(shù)據(jù)庫中不存在對應(yīng)的列。假如我們新增一列 訂單消費(fèi)金額money且該表不存在該字段

[
'attribute' => '消費(fèi)金額',
'value' => function ($model) {
// 這里可以根據(jù)該表的其他字段進(jìn)行關(guān)聯(lián)獲取
}
],

自定義行樣式

有小伙伴說了,gii生成的這個(gè)gridview表格呀,行跟行的顏色不明顯,看著難受,我滴乖乖,具體怎么難受咱們就不追究了。我們來看看怎么定義行樣式

<?= GridView::widget([
// ......
'dataProvider' => $dataProvider,
'rowOptions' => function($model, $key, $index, $grid) {
return ['class' => $index % 2 ==0 ? 'label-red' : 'label-green'];
},
// ......
]); ?>

前面的操作我們都是依據(jù)列column的,這里因?yàn)槭菍π械目刂?,所以我們配置rowOptions要稍微注意一下。此外,自定義的label-red和label-green需要有對應(yīng)的樣式實(shí)現(xiàn),這里我們看一下頁面的實(shí)際效果

增加按鈕調(diào)用js操作案例

那邊產(chǎn)品經(jīng)理走過來了,小王呀,你這個(gè)修改狀態(tài)的功能很頻繁,每次都要先點(diǎn)進(jìn)詳情頁才能修改,能不能我在列表頁面上鼠標(biāo)那么一點(diǎn)就成功修改了呢?

其實(shí)就是一個(gè)異步請求操作了當(dāng)前行的狀態(tài)嘛,我們來看看gridview里面是怎么實(shí)現(xiàn)的。

[
'class' => 'yii\grid\ActionColumn',
'header' => '操作',
'template' => '{view} {update} {update-status}',
'buttons' => [
'update-status' => function ($url, $model, $key) {
return Html::a('更新狀態(tài)', 'javascript:;', ['onclick'=>'update_status(this, '.$model->id.');']); },
],
],

我們需要在頁面寫js實(shí)現(xiàn)方法 update_status, 關(guān)于如何在頁面底部加載js請點(diǎn)擊參考

補(bǔ)充:GridView 小部件在開發(fā)中常用的功能及技巧。

數(shù)據(jù)網(wǎng)格或者說 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,
]);

一、表格列

表格的列是通過 GridView 配置項(xiàng)中的 yii\grid\GridView::columns 屬性配置的.

<?php
use yii\grid\GridView;
echo GridView::widget([
'dataProvider' => $dataProvider,
//表格列值搜索功能,注意一定要配合attribute才會顯示
//$searchModel = new ArticleSearch();
'filterModel' => $searchModel,
//重新定義分頁樣式
'layout'=> '{items}<div class="text-right tooltip-demo">{pager}</div>',
'pager'=>[
//'options'=>['class'=>'hidden']//關(guān)閉分頁
'firstPageLabel'=>"First",
'prevPageLabel'=>'Prev',
'nextPageLabel'=>'Next',
'lastPageLabel'=>'Last',
]
'columns' => [
['class' => 'yii\grid\SerialColumn'],//序列號從1自增長
// 數(shù)據(jù)提供者中所含數(shù)據(jù)所定義的簡單的列
// 使用的是模型的列的數(shù)據(jù)
'id',
'username',
// 更復(fù)雜的列數(shù)據(jù)
[
'class' => 'yii\grid\DataColumn', //由于是默認(rèn)類型,可以省略 
'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'=>'文章類別', 
/*'attribute' => 'cid',產(chǎn)生一個(gè)a標(biāo)簽,點(diǎn)擊可排序*/ 
'value' => 'cate.cname' //關(guān)聯(lián)表
],
[
//動作列yii\grid\ActionColumn 
//用于顯示一些動作按鈕,如每一行的更新、刪除操作。
'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]
);
}
],

3. 數(shù)據(jù)列有鏈接

[
'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?'男':'女'; 
},
//在搜索條件(過濾條件)中使用下拉框來搜索
'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'] 
]

腳本之家推薦閱讀:

淺析Yii2中GridView常見操作

yii2 頁面底部加載css和js的技巧

淺析Yii2 GridView 日期格式化并實(shí)現(xiàn)日期可搜索教程

淺析Yii2 GridView實(shí)現(xiàn)下拉搜索教程

以上內(nèi)容是針對Yii2中GridView常見操作的全部介紹,希望對大家有所幫助!

相關(guān)文章

  • ecshop實(shí)現(xiàn)smtp發(fā)送郵件

    ecshop實(shí)現(xiàn)smtp發(fā)送郵件

    這篇文章主要介紹了ecshop實(shí)現(xiàn)smtp發(fā)送郵件,需要的朋友可以參考下
    2015-02-02
  • 最新評論