Reputation: 17505
I need to add new, custom button to Yii2's ActionColumn. Using examples available in the Internet, I managed to create a closure for rendering custom button and it now renders with default URL.
But, I need it with a custom URL.
I managed to get this done, by overriding $url
value, that my custom button's closure function is fed, with URL generated using other function parameters, like that:
'buttons' => [
'see' => function ($url, $model, $key) {
$options = [
'title' => Yii::t('yii', 'See in frontend'),
'aria-label' => Yii::t('yii', 'See'),
'data-pjax' => '0',
];
$url = \yii\helpers\Url::toRoute(['lab/index', 'lab' => $key]);
return Html::a('<span class="glyphicon glyphicon-asterisk"></span>', $url, $options);
}
],
It works, but it isn't too professional. I wanted to use urlCreator
property, as shown in Kartik's example from Yii Forum:
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'see') {
return \yii\helpers\Url::toRoute(['lab/index', 'lab' => $key]);
}
}
But, this example is not working. It only generates correct URL for my custom button and leaves default buttons not working, without any URL. This is understandable, assuming, how urlCreator
works. But, how to fix this problem? How can I get access to ActionColumn
to use controller
property for generating URLs for default buttons or how to force it to generate these URLs for me?
Upvotes: 9
Views: 10589
Reputation: 4601
Use like that:
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'yandexCampaign') {
return Url::toRoute(['add-yandex-campaign', 'id' => $model->id]);
} else {
return Url::toRoute([$action, 'id' => $model->id]);
}
}
You need set else
case for default buttons.
Upvotes: 9