Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Yii2 GridView Action buttons

Tags:

yii2

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;
        }

       ],
like image 712
Muhammad Shahzad Avatar asked Nov 26 '14 05:11

Muhammad Shahzad


People also ask

What is actioncolumn in Yii\grid\GridView?

ActionColumn is a column for the yii\grid\GridView widget that displays buttons for viewing and manipulating the items. To add an ActionColumn to the gridview, add it to the columns configuration as follows:

How to show/hide the summary text of GridView table in yii2?

Show/Hide the summary text, footer and header of gridview table yii2.0. Hide the gridview using 'showOnEmpty' properties If the data is not available to show in gridview table. Every developer would like to play with gridview action. I added the code for that from my experience.

How to set HTML attributes for'GridView'table cells in yii2?

Configure the gridview button or template, header lable value and button options like class, style etc. Add the new button in gridview action template and configure the url, text, title etc. If we would like to set the HTML attributes for 'Gridview' table cells in yii2.0, Just use the 'contentOptions' parameter for that cell.

What is GridView in yiiframework 2?

Yii Framework 2 : Gridview. In yiiframework 2.0, The gridview widget is used to display the data like image, text, etc in a grid. It is having a lot features like sorting, pagination, filtering, styling etc. This widgets is mostly used by developers.


3 Answers

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

like image 117
rishad2m8 Avatar answered Oct 21 '22 23:10

rishad2m8


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', 
      ]) 
    : '';
like image 41
Evgeniy Tkachenko Avatar answered Oct 21 '22 23:10

Evgeniy Tkachenko


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

like image 22
Ventoh Avatar answered Oct 21 '22 22:10

Ventoh