I want to hide Yii2 GridView Action Column buttons on the base of model field status. If status is = 1 then hide view button only. How I can?
Code:
[
'class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:260px;'],
'header'=>'Actions',
'template' => '{view} {delete}',
'buttons' => [
//view button
'view' => function ($url, $model) {
return Html::a('<span class="fa fa-search"></span>View', $url, [
'title' => Yii::t('app', 'View'),
'class'=>'btn btn-primary btn-xs',
]);
},
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
$url ='/jobs/view?id='.$model->jobid;
return $url;
}
],
ActionColumn is a column for the yii\grid\GridView widget that displays buttons for viewing and manipulating the items. To add an ActionColumn to the gridview, add it to the columns configuration as follows:
Show/Hide the summary text, footer and header of gridview table yii2.0. Hide the gridview using 'showOnEmpty' properties If the data is not available to show in gridview table. Every developer would like to play with gridview action. I added the code for that from my experience.
Configure the gridview button or template, header lable value and button options like class, style etc. Add the new button in gridview action template and configure the url, text, title etc. If we would like to set the HTML attributes for 'Gridview' table cells in yii2.0, Just use the 'contentOptions' parameter for that cell.
Yii Framework 2 : Gridview. In yiiframework 2.0, The gridview widget is used to display the data like image, text, etc in a grid. It is having a lot features like sorting, pagination, filtering, styling etc. This widgets is mostly used by developers.
You can use
['class' => ActionColumn::className(),'template'=>'{view} {update}' ]
on your gridview.
Read
Just add
return $model->status == 1
? Html::a('<span class="fa fa-search"></span>View', $url, [
'title' => Yii::t('app', 'View'),
'class' =>'btn btn-primary btn-xs',
])
: '';
Use visibleButtons
property from ActionColumn
class:
[
'class' => 'yii\grid\ActionColumn',
'visibleButtons' => [
'view' => function ($model, $key, $index) {
return $model->status !== 1;
}
]
]
Reference: https://www.yiiframework.com/doc/api/2.0/yii-grid-actioncolumn#$visibleButtons-detail
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With