您现在的位置是:首页 > 电脑 > 

根据模型属性值Yii2更改Gridview中的按钮操作(Change the button action in Gridview based on a model attribute value Yii2)

2025-07-21 01:13:29
根据模型属性值Yii2更改Gridview中的按钮操作(Change the button action in Gridview based on a model attribute value Yii2) 我用CRUD生成器生成了一个简单的应用程序...... 在“视图”页面中,有一个操作列,其中包含一些按钮,如视图,更新,删除.... 我想要的只
根据模型属性值Yii2更改Gridview中的按钮操作(Change the button action in Gridview based on a model attribute value 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')]
    );
},

I generated a simple application by CRUD generator...

In the View page there is a action column assigned with some butt 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')]
    );
},

                

最满意答案

试试这个......

[ 'class' => 'yii\grid\ActionColumn', 'template' => '{activate}{deactivate}', 'butt' => [ '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; 在你看来

Try with this..

[ 'class' => 'yii\grid\ActionColumn', 'template' => '{activate}{deactivate}', 'butt' => [ '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

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/697386.html

相关标签:无
上传时间: 2023-08-27 09:50:53
留言与评论(共有 13 条评论)
本站网友 矫正牙齿过程
19分钟前 发表
'id' => $model->id]); return $url; } if ($action === 'deactivate') { $url =Url
本站网友 已有
29分钟前 发表
'deactivate' => function ($url
本站网友 苏州白癜风医院
10分钟前 发表
[ 'title' => Yii
本站网友 昆明合金公寓
23分钟前 发表
$model) { return Html
本站网友 什么是企业性质
25分钟前 发表
a('<span class="glyphicon glyphicon-eye-open"></span>'
本站网友 来个身份证
24分钟前 发表
$key
本站网友 第一茶叶网
28分钟前 发表
如视图
本站网友 成人如何提高记忆力
23分钟前 发表
$model
本站网友 资本运营
5分钟前 发表
t('app'
本站网友 腾讯年终奖
22分钟前 发表
]
本站网友 武汉舞蹈培训
3分钟前 发表
['title' => Yii
本站网友 柘林二手房
20分钟前 发表
'activate' => function($url