Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2: custom only some buttons in actions columns (other ones by default)

I would like to overload only some buttons in action columns, but when I try to do it, the default button does not work

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        (...)

        [
            'class' => 'yii\grid\ActionColumn',
            'headerOptions'=> ['style'=>'width: 70px;'],
            'template' => '{view} {update} {delete}',
            'buttons' => [
                'view' => function ($url, $model) {
                    (...)
                },
                'update' => function ($url, $model) {
                    (...)
                }
            ],
            'urlCreator' => function ($action, $model, $key) {
                if ($action === 'view') {
                    (...)
                }
                else if ($action === 'update') {
                    (...)
                }
            }
        ],
    ],
]); ?>

Using the code above, the 'delete' action doesn't work, the code generated is:

<a title="Elimina" aria-label="Elimina" data-confirm="...?" data-method="post" data-pjax="0">
    <span class="glyphicon glyphicon-trash">
    </span>
</a>

So, "delete" action is not sent and index page is re-load,

Can you help me?

like image 882
fabio Avatar asked Oct 20 '22 07:10

fabio


1 Answers

This part is causing the issue:

'urlCreator' => function ($action, $model, $key) {
    if ($action === 'view') {
        (...)
                }
    else if ($action === 'update') {
        (...)
    }
}

You did not specify url сreation for delete action button, that's why it does nothing when you click on it. Add condition into urlCreator callback for delete to generate url.

like image 93
Tony Avatar answered Oct 22 '22 20:10

Tony