问题
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} {delete} {servicestatus}',
'header'=>'<label>Action</label>',
],
来源:https://stackoverflow.com/questions/30995310/change-the-button-action-in-gridview-based-on-a-model-attribute-value-yii2