本文介绍了根据模型属性值 Yii2 更改 Gridview 中的按钮操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在查看页面中,有一个操作列分配了一些按钮,如查看、更新、删除......

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

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

'suspend' =>函数($url,$model){返回 HTML::a('<span class="btn btn-xs btn-danger icon-remove large-80"style="margin-left:5px;"></span>',$网址,['标题' =>Yii::t('app', 'Inactivate')]);},'激活' =>函数($url,$model){返回 HTML::a('<span class="btn btn-xs btn-success icon-ok large-80"style="margin-left:5px;"></span>',$网址,['标题' =>Yii::t('app', '激活')]);},
解决方案

试试这个..

['类' =>'yii\grid\ActionColumn','模板' =>'{激活}{停用}','按钮' =>['激活' =>函数($url,$model){if($model->status==1)return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, ['标题' =>Yii::t('app', '激活'),]);},'停用' =>函数($url,$model){如果($模型->状态== 0)return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, ['标题' =>Yii::t('app', '停用'),]);},],'urlCreator' =>函数($action,$model,$key,$index){if ($action === '激活') {$url =Url::toRoute(['controller/activate', 'id' => $model->id]);返回 $url;}if ($action === '停用') {$url =Url::toRoute(['controller/deactivate', 'id' => $model->id]);返回 $url;}}],

您需要在视图中包含 use yii\helpers\Url;

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 versaThis 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')]
    );
},
解决方案

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

这篇关于根据模型属性值 Yii2 更改 Gridview 中的按钮操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 23:23