根据模型属性值Yii2更改Gridview中的按钮操作

时间:2015-06-23 06:25:43

标签: php yii2

我通过CRUD生成器生成了一个简单的应用程序......

在“视图”页面中,有一个操作列,其中包含一些按钮,如视图,更新,删除....

我想要的只是创建一个状态按钮....

如果状态为非活动状态,则应询问我并将状态更改为活动状态,反之亦然 这是我的代码:

'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')]
    );
},

2 个答案:

答案 0 :(得分:4)

试试这个..

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

          }
         ],

您需要在视图中加入yii\helpers\Url;

答案 1 :(得分:0)

您还可以有条件地更改网址和图标... :)

    ['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>',
      ],