# GridView
~~~
use yii\grid\GridView;
echo yii\grid\GridView::widget([
'dataProvider' => $dataProvider,
]);
~~~
常用配置
~~~
<?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']//關閉分頁
'firstPageLabel'=>"First",
'prevPageLabel'=>'Prev',
'nextPageLabel'=>'Next',
'lastPageLabel'=>'Last',
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],//序列號從1自增長
// 數據提供者中所含數據所定義的簡單的列
// 使用的是模型的列的數據
'id',
'username',
[
'label' => '車輛ID', // 自定義顯示名稱
'attribute' => 'id', // 模型字段名稱,產生一個a標簽,點擊可排序*/
//'format' => 'html', // 內容顯示模式
//'value' => 'id', // 內容
//'value' => 'cate.cname', //關聯表
'value'=>function($model){
// 自定義處理方法
return $model->id;
},
//在搜索條件(過濾條件)中使用下拉框來搜索
'filter' => Html::activeDropDownList(
$searchModel,
'yeartype',
$array, // 搜索條件,一維數組
['prompt'=>'全部',"class" => "form-control"]
),
'headerOptions' => ['width' => '80'], // 寬度
],
// 操作按鈕自定義
[
//動作列yii\grid\ActionColumn
//用于顯示一些動作按鈕,如每一行的更新、刪除操作。
'class' => 'yii\grid\ActionColumn',
'header' => '操作',
'template' => '{view} {delete} {update}', // 展示操作
'headerOptions' => ['width' => '240'],
'buttons' => [
// 自定義屬性
'diy' => function ($url, $model, $key) {
// 按鈕屬性
$options = [
'title' => Yii::t('yii', 'View'),
'aria-label' => Yii::t('yii', 'View'),
'data-pjax' => '0',
'model'=>$model->id,
'id'=> $key,
];
// 生成按鈕
return Html::a('<span class="glyphicon glyphicon-refresh"></span>', $url, $options);
},
'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' => '你確定要刪除文章嗎?',]
]
);
},
],
],
],
]);
?>
[
'attribute' => 'is_delete',
'label' => '是否刪除',
'value'=>function ($model){
return $model->is_delete==1?'刪除':'未刪除';
},
'filter' => Html::activeDropDownList($searchModel,'is_delete',['1'=>'刪除','0'=>'未刪除'],
['prompt'=>'全部',"class" => "form-control"]),
'headerOptions' => ['width' => '100'],
],
~~~
#### **操作按鈕**
~~~
[
'class' => 'yii\grid\ActionColumn',
'header' => '操作',
'template' => '{view} {delete} {update} {diy}', // 展示操作
//'headerOptions' => ['width' => '240'],
'buttons' => [
// 自定義按鈕
'diy' => function ($url, $model, $key) {
// 按鈕屬性
$options = [
'title' => Yii::t('yii', 'View'),
'aria-label' => Yii::t('yii', 'View'),
'data-pjax' => '0',
'model'=>$model->id,
'id'=> $key,
];
// 生成按鈕
return Html::a('<span class="glyphicon glyphicon-refresh"></span>', $url, $options);
},
]
],
~~~
#### **批量刪除**
~~~
// 視圖內 - 設置表單ID ,增加ID復選:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'options' => ['id' => 'grid'],
'footer' => '<button href="#" class="btn btn-default btn-xs btn-delete" url="'. Url::toRoute('admin/delete') .'">刪除</button>',
'columns' => [
//['class' => 'yii\grid\SerialColumn'],
[
"class" => "yii\grid\CheckboxColumn",
"name" => "id",
],
]
?>
// 視圖內按鈕
<?= Html::a("批量刪除", "javascript:void(0);", ["class" => "btn btn-success gridview"]) ?>
// 視圖內注冊JS
$this->registerJs('
$(".gridview").on("click", function () {
var keys = $("#grid").yiiGridView("getSelectedRows");
console.log(keys);
$.post("delall?id="+keys);
});
');
// 控制器內定義刪除方法
public function actionDelall($id)
{
$model =new CarBrand();
if($model->deleteAll("id in($id)")){
// return $this->render('index',['id'=>3,'mark'=>2]);
return $this->redirect(['/carbrand/index']);
}else{
echo "err";
}
}
~~~
#### 1. 處理時間
~~~
[
'label'=>'更新日期',
'format' => ['date', 'php:Y-m-d'],
'value' => 'updated_at'
],
//or
[
//'attribute' => 'created_at',
'label'=>'更新時間',
'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. 數據列有鏈接
~~~
[
'attribute' => 'title',
'value' => function ($model, $key, $index, $column) {
return Html::a($model->title,
['article/view', 'id' => $key]);
},
'format' => 'raw',
],
~~~
#### 4. 數據列顯示枚舉值(男/女)
~~~
[
'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'=>'產品狀態',
'attribute' => 'pro_name',
'value' => function ($model) {
$state = [
'0' => '未發貨',
'1' => '已發貨',
'9' => '退貨,已處理',
];
return $state[$model->pro_name];
},
'headerOptions' => ['width' => '120']
]
~~~