Change the button action in Gridview based on a model attribute value Yii2

牧云@^-^@ 提交于 2020-01-14 05:46:07

问题


I generated a simple application by CRUD generator...

In the View page there is a action column assigned with some buttons like view, update, delete....

all I want is to create a status button....

If the status is inactive it should ask me and change the status into active and vice versa This is my code:

'suspend' => function($url, $model) { 
    return Html::a(
        '<span class="btn btn-xs btn-danger icon-remove bigger-80"style="margin-left:5px;"></span>',
        $url,
        ['title' => Yii::t('app', 'Inactivate')]
    );
},
'activate' => function($url, $model) {
    return Html::a(
        '<span class="btn btn-xs btn-success icon-ok bigger-80"style="margin-left:5px;"></span>',
        $url,
        ['title' => Yii::t('app', 'Activate')]
    );
},

回答1:


Try with this..

        [
          'class' => 'yii\grid\ActionColumn',
          'template' => '{activate}{deactivate}',
          'buttons' => [
            'activate' => function ($url, $model) {
            if($model->status==1)
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                            'title' => Yii::t('app', 'Activate'),
                ]);
            },
            'deactivate' => function ($url, $model) {
            if($model->status==0)
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                            'title' => Yii::t('app', 'Deactivate'),
                ]);
            },

          ],
          'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'acivate') {
                $url =Url::toRoute(['controller/activate', 'id' => $model->id]);
                return $url;
            }
           if ($action === 'deactivate') {
                $url =Url::toRoute(['controller/deactivate', 'id' => $model->id]);
                return $url;
            }

          }
         ],

You need to include use yii\helpers\Url; in your view




回答2:


You May also Change Url and Icon Conditionally...:)

    ['class' => 'yii\grid\ActionColumn',
                'buttons'=>[
                        'servicestatus' => function ($url, $model) 
                        {
                            if($model->service_status =="Paid")
                            {
                              return Html::a(
                                '<span class="glyphicon glyphicon-remove red"></span>',
                                ['service-payment/status', 'id' => $model->id], 
                                [
                                    'title' => 'Status',
                                    'data-pjax' => '0',
                                ]
                            );
                        }else
                        {
                            return Html::a(
                                '<span class="glyphicon glyphicon-ok green"></span>',
                                ['service-payment/status', 'id' => $model->id], 
                                [
                                    'title' => 'Status',
                                    'data-pjax' => '0',
                                ]
                            );
                        }
                        },
                ],
                 'template'=>'{view}&nbsp;&nbsp;{delete}&nbsp;&nbsp;{servicestatus}',


                  'header'=>'<label>Action</label>',
      ], 


来源:https://stackoverflow.com/questions/30995310/change-the-button-action-in-gridview-based-on-a-model-attribute-value-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!