ActionColumn's button with custom URL
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.
trejder
I'm just an average person, that uniquely combines strict, engineering mind with a non-schematic, humanistic and magical way of looking on life and surrounding world. Add a little bit of madness, and you'll get entire picture of me! :] I'm a developer, both desktop and mobile, with 15+ years of experience and 50+ small-to-big projects on account. In the very same time I'm a writer with similar experience and a lot of books, articles, blog posts or own blogs ideas on account.
Updated on July 25, 2022Comments
-
trejder almost 2 years
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 toActionColumn
to usecontroller
property for generating URLs for default buttons or how to force it to generate these URLs for me?