-
-
Save almirb/422ae66c6dcbed89731e149998d6f21e to your computer and use it in GitHub Desktop.
Yii 2: Ajax + Delete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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