Yii2 Pjax Delete does not work

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, ]); } 
+6
source share
7 answers

data-method and data-confirm do not allow you to create an ajax request via pjax, you must execute your own confirmation dialog and remove the POST-verb filter, or you can implement your own ajax plugin with a confirmation dialog and specify the http method.

Also, I think there should be a way to extend the pjax plugin through the confirmation dialog, but Yii2 does not provide this by default.

+3
source

first remove the 'data-confirm' and 'data-method' => 'post'. pjax is not going to work. If you want to implement a confirmation window with an action button, this is what I would like to do in my index.php file.

 <?php Pjax::begin(['id' => 'pjax-container']); echo GridView::widget([ 'test' => function ($url, $dataProvider) { return Html::a('Test', ['/site/test'], ['title'=>'Test', 'onclick' => "if (confirm('ok?')) { $.ajax('/site/test', { type: 'POST' }).done(function(data) { $.pjax.reload({container: '#pjax-container'}); }); } return false; ", ]); }, ]) Pjax::end(); ?> 

and in my controller

 public function actionTest() { if (!Yii::$app->request->isAjax) { return $this->redirect(['index']); } } 

This way you will have confirmation, etc. If you want, you can use other third-party download confirmation, etc. And you will work fine.

+2
source
 <?php Pjax::begin(['id' => 'model-grid']); 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, //... [ 'class' => 'yii\grid\ActionColumn', 'template' => '{update} {delete}', 'contentOptions' => ['class' => 'action-column'], 'buttons' => [ 'delete' => function ($url, $model, $key) { return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [ 'title' => 'Delete', 'data-pjax' => '#model-grid', ]); }, ], ], ], ]); Pjax::end(); ?> 

In the controller

 public function actionDelete($id) { $this->findModel($id)->delete(); $searchModel = new ModelSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } 
+2
source

First add Pjax::end(); at the end of the gridview then specify:

  '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', ]); }, 

Note that you do not need to specify 'data-pjax' => '0' because it disables the pjax link.

See link for more details.

Your controller should be:

 public function actionDelete($id) { $this->findModel($id)->delete(); return $this->redirect(['index']); } 
0
source

Try changing actionDelete ()

 public function actionDelete($id) { $this->findModel($id)->delete(); return \yii\web\Response::redirect(['index'] , 302 , false); // return $this->redirect(['index']); } 

because Controller-> redirect () cannot disable ajaxCheck, for this you need Response.

I created the same problem at https://github.com/yiisoft/yii2/issues/11058 .

0
source

Do not set data-method and data-confirm because Pjax not supported.

After you removed it is still not fake, but because of the code below your controller does not allow Pjax get Request.

 return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], // **remove this** ], ], ]; 

You need to use the Pjax Post method. Apply this in your Pjax.

 'clientOptions' => ['method' => 'POST'] 
0
source

It can be used as follows:

in sight:

 'delete' => function ($url, $model, $key) { $options = [ 'title' => Yii::t('common', 'delete'), 'aria-label' => Yii::t('common', 'delete'), 'data-pjax' => 'w0',//id 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), 'data-method' => 'post', 'class' => 'btn btn-xs btn-danger' ]; return Html:: a('<i class="fa fa-fw fa-trash"></i>', [ 'delete', 'id' => $model -> id ], $options); } 

in the controller:

 $this -> findModel($id) -> delete (); $searchModel = new AdminSearch(); //get the referer url $url = Yii::$app -> request -> referrer; $arr = parse_url($url, PHP_URL_QUERY); parse_str($arr, $output);//get the $_GET array $dataProvider = $searchModel -> search($output); return $this -> render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); 
0
source

Source: https://habr.com/ru/post/984672/


All Articles