Blackjack
Blackjack

Reputation: 1065

Yii2 Gridview Dropdown Button in Action column

I want to make my action column button as dropdown button in my gridview, this is my code

// ... GridView configuration ...
['class' => 'yii\grid\ActionColumn',               
            'template' => '{sell} {delete}',
            'buttons' => [
                'sell' => function ($url, $model) {
                    return Html::a('<button type="button" class="btn btn-success"><i class="glyphicon glyphicon-shopping-cart"></i></button>', $url, [
                                'title' => Yii::t('app', 'Sell Tickets'),
                                'data-toggle' => "modal",
                                'data-target' => "#myModal",
                                'data-method' => 'post',
                    ]);
                },
                'delete' => function ($url, $model) {
                    return Html::a('<button type="button" class="btn btn-danger"><i class="glyphicon glyphicon-remove-sign"></i></button>', $url, [
                                'title' => Yii::t('app', 'Delete'),
                                'data-toggle' => "modal",
                                'data-target' => "#myModal",
                                'data-method' => 'post',
                    ]);
                },
            ],
            'urlCreator' => function ($action, $model, $key, $index) {
                if ($action == 'sell') {
                    $url = Url::toRoute(['trip/sell', 'id' => $model->tripScheduleId]);
                    return $url;
                } else {
                    $url = Url::toRoute(['trip/delete', 'id' => $model->tripScheduleId]);
                    return $url;
                }
            },
        ],

and this is the view

enter image description here

I've followed many in so or any source but nothing work on me.

Upvotes: 0

Views: 502

Answers (1)

Michaelsoft
Michaelsoft

Reputation: 807

buttons is an array of rendering function, just render your dropdown there and remove the urlCreator section.

urlCreator is used to build URLs for the default buttons in the template so by rendering a dropdown with the right URLs you won't need it.

'template' => '{actions}',
'buttons' => [
    'actions' => function ($url, $model) {
        //return you dropdown here
    }
],

Just don't care about $url parameter and create your URLs inside the function.

Upvotes: 1

Related Questions