How to remove the buttons from the grid please?

Hello,

I would like to know if there is a way to remove the buttons in the grid(update, delete) based on the access rules set in the controller as:

[php]

        'access' => [


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


             'only' => ['create', 'read', 'update', 'delete'],


             'rules' => [


                [


                    'allow' => true,


                    'actions' => ['create', 'read', 'update'],


                    'roles' => ['@'],





                ],


                      ],


              ],

/php]

Also if you have another example based on the user role please.

I found a few examples on the internet but still do not understand, if you have an easy way of explaining it , I would be grateful!

Thank you so much.

Ben

well i did this:

in vendor\yiisoft\yii2\grid\actioncolumn




protected function initDefaultButtons()

    {  if(\Yii::$app->user->can('admin'))       

      {

        if (!isset($this->buttons['view'])) {

            $this->buttons['view'] = function ($url, $model, $key) {

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

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

                    'data-pjax' => '0',

                ]);

            };

        }

        if (!isset($this->buttons['update'])) {

            $this->buttons['update'] = function ($url, $model, $key) {

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

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

                    'data-pjax' => '0',

                ]);

            };

        }

        if (!isset($this->buttons['delete'])) {

            $this->buttons['delete'] = function ($url, $model, $key) {

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

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

                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),

                    'data-method' => 'post',

                    'data-pjax' => '0',

                ]);

            };

        }

      }

	

	

 else {     

          if (!isset($this->buttons['view'])) {

            $this->buttons['view'] = function ($url, $model, $key) {

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

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

                    'data-pjax' => '0',

                ]);

            };

        }

      }

    }



I don,t if this the best way but it works for me, an this is global too

In general you have to extend a class not overwrite.

Create a file wherever you want, for example in components/grid/ActionColumn.php




<?php

namespace app\components\grid;

use yii\helpers\Url;


class ActionColumn extends \yii\grid\ActionColumn {

   //here you can overwrite the functions of the parent class

}



and in you gridview call to this actionColumn




    ...

    'class' => 'app\components\grid\ActionColumn',

    ...



Vendor folder can be changed by composer update and youll lose everithing there.

Better customize the button in gridview and actions in controller based on roles

Maybe you could also try to use the "visible" attribute of the ActionColumn?

Example:




        [

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

            'visible' => Yii::$app->user->isGuest ? false : true,

        ],



Regards

Hello all,

I have found a very way of doing this:





   <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

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


            'id',

            'title',

            'author',


            ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {delete} {update} '], <--------remove the one you don't want.

        ],

    ]); ?>



This will make it global of course.

It works very well!

Thanks again,

Ben