Skip to content

Instantly share code, notes, and snippets.

@almirb
Forked from jamband/PostController.php
Last active September 29, 2017 18:37
Show Gist options
  • Save almirb/422ae66c6dcbed89731e149998d6f21e to your computer and use it in GitHub Desktop.
Save almirb/422ae66c6dcbed89731e149998d6f21e to your computer and use it in GitHub Desktop.
Yii 2: Ajax + Delete
<?php Pjax::begin(['id' => 'pjax-container']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
// ...
[
'class' => 'yii\grid\ActionColumn',
'buttons' => [
'delete' => function ($url) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', '#', [
'title' => Yii::t('yii', 'Delete'),
'aria-label' => Yii::t('yii', 'Delete'),
'class' => ['btn btn-sm btn-danger'],
'onclick' => "
krajeeDialog.confirm('". Yii::t('kvgrid', 'Are you sure to delete this item?') ."', function (result) {
if (result) {
$.ajax('$url', {
type: 'POST'
}).done(function(data) {
$.pjax.reload({container: '#kv-pjax-container'});
});
}
});
",
]);
},
],
],
],
]); ?>
<?php Pjax::end() ?>
<?php
class PostController extends Controller
{
// ...
public function actionDelete($id)
{
$this->findModel($id)->delete();
if (!Yii::$app->request->isAjax) {
return $this->redirect(['index']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment