Hide Yii2 GridView Action buttons

后端 未结 10 553
失恋的感觉
失恋的感觉 2021-02-03 21:20

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:

              


        
相关标签:
10条回答
  • 2021-02-03 21:29

    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;
                    }
                    }
    ],
    
    0 讨论(0)
  • 2021-02-03 21:30

    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;
             },
        ]
    ],
    
    0 讨论(0)
  • 2021-02-03 21:30

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

    0 讨论(0)
  • 2021-02-03 21:40

    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}'],
    0 讨论(0)
  • 2021-02-03 21:46

    You need to change one line only.

    Replace:

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

    With:

    'template' => function($model){
       return ($model->status==1)?'{update} {delete}':'{view} {update} {delete}';
    },

    0 讨论(0)
  • 2021-02-03 21:48

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

    0 讨论(0)
提交回复
热议问题