Skip to content

Instantly share code, notes, and snippets.

@jamband
Created May 26, 2015 11:02
Show Gist options
  • Save jamband/cf1ab228a708c5fdcdbc to your computer and use it in GitHub Desktop.
Save jamband/cf1ab228a708c5fdcdbc 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(Yii::t('yii', 'Delete'), '#', [
'title' => Yii::t('yii', 'Delete'),
'aria-label' => Yii::t('yii', 'Delete'),
'onclick' => "
if (confirm('ok?')) {
$.ajax('$url', {
type: 'POST'
}).done(function(data) {
$.pjax.reload({container: '#pjax-container'});
});
}
return false;
",
]);
},
],
],
],
]); ?>
<?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']);
}
}
}
@gugoan
Copy link

gugoan commented Jun 4, 2015

Man, it work very goood for me.

@sanicode
Copy link

many thanks..

@MrMizerakl
Copy link

thanks...

@nadirvishun
Copy link

We can also do this use pjax:
view:

<?php Pjax::begin(['id' => 'pjax-container']) ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            // ...
            [
                'class' => 'yii\grid\ActionColumn',
                'buttons' => [
                    'delete' => function ($url) {
                        return Html::a(Yii::t('yii', 'Delete'), '#', [
                            'title' => Yii::t('yii', 'Delete'),
                            'aria-label' => Yii::t('yii', 'Delete'),
                            'data-pjax' => 'pjax-container',//pjax
                            'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                            'data-method' => 'post',
                        ]);
                    },
                ],
            ],
        ],
    ]); ?>
<?php Pjax::end() ?>

controller:

<?php
class PostController extends Controller
{
    // ...
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        $searchModel=new PostSearch();//the model
        $url=Yii::$app->request->referrer;//the referer url
        $arr = parse_url($url, PHP_URL_QUERY);
        parse_str($arr, $output);//get $_GET array
        $dataProvider = $searchModel->search($output);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
}

@qbss1221
Copy link

Why this is Hard delete not Soft delete in DB?

@ukccatc
Copy link

ukccatc commented Jun 24, 2018

Thanks a lot!

@tiagomv94
Copy link

nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment