More than one condition in "visible" ActionColumn class

Hello,

I need one little help, please.

In a gridview:




    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            [

            	'attribute' => 'id',

            	'visible' => (Yii::$app->user->can("admin")),

            	'options' => ['width' => '70']

			],

			[

            	'attribute' => 'status_id',

            	'label' => 'Stato',

            	'filter' => Umarkerstat::dropdown(),

            	'value' => function($model, $index, $dataColumn) {

            		$umarkerstatDropdown = Umarkerstat::dropdown();

            		return $umarkerstatDropdown[$model->status_id];

            	}

            ],

			...



The GridView end with the Actioncolumn where is need to show some icons only if a user is admin (I use Yii2 user authentication module of amnah)




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

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

				'header' => 'Azioni',

           		'buttons' => ['publish' => function ($url, $model) {

					return Html::a('<span class="glyphicon glyphicon-pushpin"></span>', $url, [

	                    'title' => Yii::t('app', 'Publish'), ]);

				}],

				'visible' => (Yii::$app->user->can("admin")),

				'options' => ['width' => '120'],

			],

           	[

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

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

				'visible' => (!Yii::$app->user->can("admin")),

				'options' => ['width' => '120'],

			],

           ],

		]); ?>



Now my problem is that I need to show icons {view}{update}{delete}{publish} if the user can admin, but to hide if "status_id" is == "1" too.

More or less:




'visible' => (Yii::$app->user->can("admin") && $dataProvider->status_id == 1 ),



But I have no idea how to write this (or better way) in the right way. Can someone help me, please?

Hi,

There is a solution for you:

  • Create a column with format following:

[

‘format’ => ‘raw’,…,

‘value’ => function($data){

return Html::a(‘View’,$urlView,[]).PHP_EOL.Html::a(‘Update’,$urlUpdate,[]).PHP_EOL…;

}

]

With the column type above, you can custom your issues easily and clearly.

Good luck.