Showing posts with label ActionColumn. Show all posts
Showing posts with label ActionColumn. Show all posts

Thursday, February 18, 2016

Hide buttons in gridview depending on variable

If in Yii2 you want to customize ActionColumn on Gridview widget depending on variable, e.g. status = 1, then you could use this code:

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

            //view button
            'view' => function ($url, $model) {
                return Html::a('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;
            }
        }

       ],
Source and discussion

Another sample:

// Comment
['class' => 'yii\grid\ActionColumn',
    'template' => '{update} {delete}',
    'buttons' => [
        'delete' => function ($url, $model, $key) {
            if ($model->fk_status_id == 1) {
                return Html::a('<span class=\'glyphicon glyphicon-trash\'></span>', $url, ['data-pjax' => 0, 'data-method' => 'post', 'data-confirm' => 'Are you sure you want to delete this item?', 'aria-label' => 'Delete', 'title' => 'Delete']);
            }
         },
    ],
],