34

I want to hide Yii2 GridView Action Column buttons on the base of model field status. If status is = 1 then hide view button only. How I can?

Code:

     [  
        'class' => 'yii\grid\ActionColumn',
        'contentOptions' => ['style' => 'width:260px;'],
        'header'=>'Actions',
        'template' => '{view} {delete}',
        'buttons' => [

            //view button
            'view' => function ($url, $model) {
                return Html::a('<span class="fa fa-search"></span>View', $url, [
                            'title' => Yii::t('app', 'View'),
                            'class'=>'btn btn-primary btn-xs',                                  
                ]);
            },
        ],

        'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'view') {
                $url ='/jobs/view?id='.$model->jobid;
                return $url;
        }

       ],
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130

10 Answers10

34

You can use ['class' => ActionColumn::className(),'template'=>'{view} {update}' ] on your gridview.

rishad2m8
  • 1,365
  • 20
  • 27
23

Use visibleButtons property from ActionColumn class:

[
    'class' => 'yii\grid\ActionColumn',
    'visibleButtons' => [
        'view' => function ($model, $key, $index) {
            return $model->status !== 1;
         }
    ]
]

Reference: https://www.yiiframework.com/doc/api/2.0/yii-grid-actioncolumn#$visibleButtons-detail

rob006
  • 21,383
  • 5
  • 53
  • 74
Ventoh
  • 512
  • 5
  • 8
21

Read

Just add

return $model->status == 1 
    ? Html::a('<span class="fa fa-search"></span>View', $url, [ 
        'title' => Yii::t('app', 'View'),
        'class' =>'btn btn-primary btn-xs', 
      ]) 
    : '';
robsch
  • 9,358
  • 9
  • 63
  • 104
Evgeniy Tkachenko
  • 1,733
  • 1
  • 17
  • 23
9

It can be done like this

[
    'class' => 'yii\grid\ActionColumn',
    'contentOptions' => [],
    'header'=>'Actions',
    'template' => '{view} {update} {delete}',
    'visibleButtons'=>[
        'view'=> function($model){
              return $model->status!=1;
         },
    ]
],
user3410311
  • 582
  • 4
  • 6
7

In yii2 use return Url::to(['controler/action']);

altogether

        [
        'class' => 'kartik\grid\ActionColumn',
        'template' => '{today_action}',
        'buttons' => [
                        'today_action' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-check"></span>', $url, 
                        [
                            'title' => Yii::t('app', 'Change today\'s lists'),
                        ]);
                    }
                ],
                'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'today_action') {
                return Url::to(['customers/today']);
            }
        }
            ],

I am using kartik extension but works fine with yii

open-ecommerce.org
  • 1,763
  • 1
  • 25
  • 41
3

you need to add the template propiety ('template'=>'{update} {delete}') to the column arry where you put the options

    'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id', 
            'otherfield'
['class' => 'yii\grid\ActionColumn','template'=>'{update} {delete}'],
1

This is what I have done https://github.com/Mihai-P/yii2theme-brain/blob/master/widgets/ActionColumn.php in short I have extended the ActionColumn class and use my own instead of the default one. my class has more things in it, like checking for access privileges and showing only the buttons they have access to, you can ignore that part and just use the way to check for the way to check for the model. I consider this more reusable then writing code in the view. If you start writing code in the view then you have to write the same code over and over again for each screen.

I am sure you can also do what you want inside the view, try using

'template' => function ($model) {
            .............
        }

And return either '{view} {delete}' or '{delete}'

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
1

You need to change one line only.

Replace:

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

With:

'template' => function($model){
   return ($model->status==1)?'{update} {delete}':'{view} {update} {delete}';
},
Ankit Singh
  • 922
  • 9
  • 16
0

Its works for me:

         'buttons' => [
            'view' => function ($url, $model) {
                return $model->status == '' ? Html::a('<span class="fa fa-paperclip fa-fw fa-border"></span>', $url, [
                            'title' => Yii::t('app', 'Visualizar'),
                            //'class'=>'btn btn-primary btn-xs',                                  
                ]) : '';
            },

is the same as: return $model->status == '' ? 'show_action_here' : 'no_show';

gugoan
  • 770
  • 2
  • 11
  • 35
0

this one worked for me . complete ActionColumn code

[  
                'class' => 'yii\grid\ActionColumn',
                'contentOptions' => ['style' => 'width:260px;'],
                'header'=>'Actions',
                'template' => '{view}',
                'buttons' => [

                    //view button
                    'view' => function ($url, $model) {
                        return  Html::a('<span class="fa fa-search"></span>View', $url, 
[ 'title' => Yii::t('app', 'View'), 'class'=>'btn btn-primary btn-xs', ]) ;
                    },
                ],

                'urlCreator' => function ($action, $model, $key, $index) {
                    if ($action === 'view') {
                        $url = \yii\helpers\Url::toRoute(['general-info/viewalldetails', 'id' => $key]);
                        return $url;
                }
                }
],
Shuhad zaman
  • 3,156
  • 32
  • 32