Gridview: Hiding Or Showing Actioncolumn Based On Model Value

Hi,

I would like to hide a column based on the value of a property of the model.

For example, in the list of POST I should see the button "edit" only for those "in draft":




GridView::widget([

    'dataProvider' => $dataProvider,

...

[

  'class' => 'yii\grid\ActionColumn',

  'template' => '{update}',

  'visible' => <isDraft> //<-- Here the value from model->getIsDraft()

],

...



Which is the best way to do this?

So what you actually want is to hide the button, not the colum. This is possible when you define your own button and return an empty string when it should not be displayed:




[

  'class' => 'yii\grid\ActionColumn',

  'template' => '{update}',

  'buttons' => [

     'update' => function ($url, $model) {

                return $model->isDraft ? Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [

                    'title' => Yii::t('yii', 'Update'),

                    'data-pjax' => '0',

                ]) : '';

            };

  ],

],