Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionColumn's button with custom URL

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?

like image 407
trejder Avatar asked Apr 15 '15 06:04

trejder


1 Answers

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.

like image 66
vitalik_74 Avatar answered Oct 18 '22 23:10

vitalik_74