How to modify the grid view actionColum's template property dynamically based on the data value?

Hello everybody,

I would like to change the template property of ‘actionColoum’ dynamically based on the id column value.

default template value looks like this ‘template’=>’{view}{update}{delete}’

when/if $userIdentity->id === $gridrow->id the template should be like this ‘template’=> ‘{view}{update}’

delete option must be removed so user can not delete his own record.

is it possible or any alternative method available?

Thanks

As far as I know, all the rows share the single template. It is fixed for the whole grid.

But you can control the rendering of the buttons individually per each row.

http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$buttons-detail

In your case, something like this:




[

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

    'template' => '{view} {update} {delete}',

    'buttons' => [

         'delete' => function ($url, $model, $key) {

              return $model->id === Yii::$app->user->id ? '' : Html::a('delete', $url);

         },

    },

]



Thank you Softark.

Your suggestion will fix my issue.

In your code you have given $url,$model,$key as the parameters to a closure.

I also noticed in some example ‘value’=> closure function can take “$data” parameter.

Where is this $data defined or coming from?.

What are other possible parameter (predefined variable/object (Ex-$data) ) available for this closure function?

Is there any documentation available?

Thanks

I think that ‘$data’ is for Yii 1.1.x.

On the topmost line of every page of the Yii Framework site, you can find a list of links that lead you to the official documentation. "guide 2.0" and "api 2.0" are the most important.

Thank you Saftark.