Yii2: custom only some buttons in action columns (others by default)

I would like to overload only some buttons in the action columns, but when I try to do this, 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 does not work, the generated code:

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

So, the delete action is not sent, and the index page is reloaded,

Can you help me?

+5
source share
1 answer

This part causes the problem:

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

You did not provide a URL for the delete action button, so it does nothing when you click on it. Add the condition to the urlCreator for delete to generate the url.

+3
source

All Articles