[Solved] editicon in indexview only if owner

Hi

I’m new to yii(2) and it’s a bit confusing.

I’ve got a lot working now, but one thing I can’t find out.

For example, on indexview, there are this icons to show,edit,delete a row.

I can’t find, where the logic is, to ‘switch on’ - ‘switch off’ these icons.

I want only edit and delete the row, if the logged-in user is the owner.

I is no problem for me with the database, I can also get the userID.

But the ‘switch on’ - ‘switch off’ of the icon/link is a miracle for me '(at the moment ;)

So I hope, it’s not for you, and somebody can give me a good solution.

Thank you,

Andi

You can set the template of the ActionColumn like this:


    

[

  'class' => ActionColumn::className(),

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

],

This way you cann add/remove/reorder/… the shown icons.

Hope this helps you a little bit with your problem :)

Thank you!

I can set it by calling the widget in view with parameter


['class' => 'yii\grid\ActionColumn', 'template' => '{view} {update}'],

as you described and it is mentioned in


vendor/yiisoft/yii2/grid/ActionColumn.php

and it works.

But I want to be able to change/delete only one specific row (row belongs to logged user).

So I have to go through the datarows? It should come from a model?

I think it is not a good way to change code in the ActionColumn.php file.

Isn’t it something like a library? - You know, I’m new to yii and (I’m sure), I didn’t realy understand the concept by now.

Thank you.

No, you can define this at the point where you implement your GridView:


echo GridView::widget([

      'dataProvider' => $dataProvider,

      'filterModel' => $searchModel,

      'columns' => [

        'id',

        'name',

        'city',

        [

          'class' => ActionColumn::className(),

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

        ],

       ]);




Thank you.

I found that, but it’s only for all rows.

So I think, I have to make the view by hand and not using the widget.

Thank you.

I found the solution.

e.g. in INDEX view in widget like ‘dkscr’ wrote


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

is for all rows. You will see ‘{view} {update} {delete}’ in this order in actioncolumn.

but you can implement some logic.


......	

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

'buttons' => [

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

		if ($model->owner=== \Yii::$app->user->identity->id ){

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

			['title' => Yii::t('app', 'Update'),

			]);

		}

	},

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

		if ($model->owner=== \Yii::$app->user->identity->id){

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

			['title' => Yii::t('app', 'Delete'),

			]);

		}	

	}

],

......

Then ‘update’ and ‘delete’ are only visible when the field ‘owner’

in the row is the same as the ‘id’ of the logged-in user.

‘view’ is always displayed.

For other icons change


class="glyphicon glyphicon-trash"

to something like


class="glyphicon glyphicon-check"