我在Yii2上遇到了一个问题(和往常一样)。我试图在GridView中的一个单元格中显示许多按钮。我有一个票据,有四种可能的状态。
在一列(ActionColumn?)中,我想显示许多按钮来改变它的状态,就像这样。
这是我的GridView中那一列的代码。我只能显示一个按钮,不知道怎么做才能显示更多的按钮,因为你不能返回多个元素,或者数组。
[
'label' => 'Change State:',
'format' => 'raw',
'value' => function($dataProvider){
if($dataProvider->state== '1'){
return Html::a('In Process', ['/tickets/inprocessticket', 'id' => $dataProvider->id], ['class'=>'btn btn-warning', 'id' => 'btn_inProcessTicket']);
}else if($dataProvider->state== '2'){
return Html::a('Close Ticket', ['/tickets/closeticket', 'id' => $dataProvider->id], ['class'=>'btn btn-danger', 'id' => 'btn_closeTicket']);
}else if($dataProvider->state== '3'){
return Html::a('Edit Ticket', ['/tickets/editticket', 'id' => $dataProvider->id], ['class'=>'btn btn-info', 'id' => 'btn_editTicket']);
}else if($dataProvider->state== '4'){
return Html::a('Close Ticket', ['/tickets/closeticket', 'id' => $dataProvider->id], ['class'=>'btn btn-danger', 'id' => 'btn_closeTicket']);
}
},
],
谢谢大家的帮助
如果你想使用 yii\grid\ActionColumn
你应该在 $buttons
财产和用途 $visibleButtons
回调来决定哪些按钮应该被显示。您可以使用 $template
属性来设置它们的顺序。
[
'class' => \yii\grid\ActionColumn::class,
'template' => '{process} {edit} {close}' //here will be all posible buttons
'buttons' => [
'process' => function($url, $model, $key) {
return Html::a(
'In Process',
[
'/tickets/inprocessticket',
'id' => $model->id
],
[
'class'=>'btn btn-warning',
'id' => 'btn_inProcessTicket'
]
);
},
'edit' => function($url, $model, $key) {
return Html::a(
'Edit Ticket',
[
'/tickets/editticket',
'id' => $dataProvider->id
],
[
'class'=>'btn btn-info',
'id' => 'btn_editTicket'
]
);
},
'close' => function ($url, $model, $key) {
return Html::a(
'Close Ticket',
[
'/tickets/closeticket',
'id' => $dataProvider->id
],
[
'class'=>'btn btn-danger',
'id' => 'btn_closeTicket'
]
);
},
],
'visibleButtons' => [
'process' => function($model, $key, $index) {
//the in process button should only be shown if state == 1
return $model->state == 1;
},
'edit' => function($model, $key, $index) {
//the edit button should only be shown if state == 3
return $model->state == 3;
},
'close' => function($model, $key, $index) {
//the close button should be shown in each state except of state == 3
return $model->state != 3;
},
],
]
你可以找到更多关于 ActionColumn
和它的回调 在文件中.