I am trying to create an Ajax GridView using Pjax with the delete button. Removal occurs without Ajax. I am new to Yii2, so any help would be appreciated. Thanks.
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 the public actionIndex () function 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, ]); }
source share