是否显示某列
代码实现如下:
[
"attribute" => "name",`这里写代码片`
"visible" => true,
],
//自定义值
[
"attribute" => "create_time",
"value" => function ($model) {
return date("Y-m-d H:i:s", $model->create_time);
},
],
显示图片
[
"label" => "封面图",
"format" => [
"image",
[
"width"=>"100",
"height"=>"100"
]
],
"value" => function ($model) {
return $model->image;
}
],
html渲染, 不做Html::encode()处理;
[
"attribute" => "title",
"value" => function ($model) {
return $model->content;
},
"format" => "raw",
],
自定义按钮案例//$url 为Grid类拼接当前控制器名称/按钮模版名称/当前行模型id controllerName/buttons-template-name/$model->id
[
"class" => "yii\grid\ActionColumn",
"template" => "{buttons-template-name} {view} {update}",
"header" => "查看 修改 删除 其他",
"buttons" => [
"buttons-template-name" => function ($url, $model, $key) {
return Html::a("text-contenx", $url, ["title" => "xyz"] );
},
],
],
设定宽度
[
"attribute" => "title",
"value" => "title",
"headerOptions" => ["width" => "100"],
],
只需要指定配置项headerOptions即可。
自定义字段
[
"attribute" => "自定义字段",
"value" => function ($model) {
return $model->xyz;
}
],
样式
<?= GridView::widget([
"dataProvider" => $dataProvider,
"rowOptions" => function($model, $key, $index, $grid) {
return ["class" => $index % 2 ==0 ? "label-red" : "label-green"];
},
]); ?>
增加按钮调用js
[
"class" => "yii\grid\ActionColumn",
"header" => "修改",
"template" => "{view} {update} {edit-isHot}",
"buttons" => [
"edit-isHot" => function ($url, $model, $key) {
return Html::a("更新状态", "javascript:;", ["onclick"=>toggle_isHot(this, ".$model->id.");"]); },
],
],
禁止表头排序
$dataProvider = new ActiveDataProvider([ "query" => $query, ]);
$dataProvider->setSort(false);
表格表头
echo GridView::widget([
//......
'caption' => '内容管理',
'captionOptions' => ['style' => 'font-size: 16px; font-weight: bold; color: #000; text-align: center;'],
]);
字段内容换行
[
'attribute' => 'api_url',
'contentOptions' => ['style' => 'white-space: normal;', 'width' => '20%'],
],