Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Pjax Delete not working

Tags:

php

crud

yii2

pjax

I am trying to make an Ajax GridView using Pjax with delete button. Deleting goes with no Ajax. I am new to Yii2 so any help would be appreciated. Thank you.

index.php

<?php Pjax::begin(['id' => 'countries']) ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'title',


        ['class' => 'yii\grid\ActionColumn',
            'buttons' => [
                'delete' => function ($url, $model, $key) {
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                        'title' => Yii::t('yii', 'Delete'),
                        'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                        'data-method' => 'post',
                    ]);
                },
            ]
        ],
    ],
]); ?>
<?php Pjax::end() ?>

Controller

public function actionDelete($id)
{   
    $model = new Category();
    $this->findModel($id)->delete();
    $dataProvider = new ActiveDataProvider([
        'query' => Category::find(),
    ]);
    return $this->render('index', [
        'dataProvider' => $dataProvider,
        'model' => $model,
    ]);
}

This is public function actionIndex() in the Controller

public function actionIndex()
{
    $model = new Category();

    $dataProvider = new ActiveDataProvider([
        'query' => Category::find(),
    ]);

    if ($model->load(Yii::$app->request->post()) && $model->save())
    {
        $model = new Category();
    }
    return $this->render('index', [
        'dataProvider' => $dataProvider,
        'model' => $model,
    ]);
}
like image 362
игорь88 Avatar asked Apr 03 '15 23:04

игорь88


1 Answers

data-method and data-confirm don't let you create ajax request via pjax, you should implements your own confirmation dialog and drop POST verb filter, or you can implements your own ajax plugin with confirmation dialog and specifying http method.

Also, i think, there must be way to extend pjax plugin by confirmation dialog, but Yii2 don't provide this by default.

like image 123
xskif Avatar answered Sep 19 '22 01:09

xskif