Hide Yii2 GridView Action buttons

后端 未结 10 554
失恋的感觉
失恋的感觉 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:50

    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

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

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

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

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

    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

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