Yii 2: Pjax + Gridview delete does not send ajax request

I am using Pjax with Gridview and I want all my action button to do ajax. By default, they do not work, so I googled and found a way to remove data-pjax = 0. But still there are no ajax requests, all of them are regular requests.

Many people have this problem, and I also could not find a solution.

I followed:

My code is:

<?php Pjax::begin(['id' => 'employee-timesheet-grid-id', 'timeout' => false, 'enablePushState' => false, 'clientOptions' => ['method' => 'POST']]) ?> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], [ 'label' => 'Employee', 'value' => function ($model) { return $model->employeePayRate->employeeName; }, ], [ 'class' => 'yii\grid\ActionColumn', 'template' => '{view} {delete}', 'buttons' => [ 'delete' => function ($url , $model) { return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, ['data-confirm' => 'Are you sure you want to delete this item?', 'data-method' =>'POST'] ); } ], 'urlCreator' => function ($action, $model, $key, $index) { if ($action === 'view') { $url = Url::to(['employee-time-sheet/view', 'id' => $model->id]); return $url; } else if ($action === 'delete') { $url = Url::to(['employee-time-sheet/delete', 'id' => $model->id]); return $url; } } ], ], ]); ?> <?php Pjax::end(); ?> 

Has anyone found a solution to this problem yet?

+6
source share
2 answers

Try it. ( my working code )

The first case following JavaScript in the above Gridview file. Here I use the bootbox confirmation window .

 $this->registerJs(" $(document).on('ready pjax:success', function () { $('.ajaxDelete').on('click', function (e) { e.preventDefault(); var deleteUrl = $(this).attr('delete-url'); var pjaxContainer = $(this).attr('pjax-container'); bootbox.confirm('Are you sure you want to change status of this item?', function (result) { if (result) { $.ajax({ url: deleteUrl, type: 'post', error: function (xhr, status, error) { alert('There was an error with your request.' + xhr.responseText); } }).done(function (data) { $.pjax.reload({container: '#' + $.trim(pjaxContainer)}); }); } } ); }); }); "); 

And below is the code for Gridview

  <?php \yii\widgets\Pjax::begin([ 'id' => 'pjax-list', ]); ?> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'stu_category_name', [ 'class' => 'yii\grid\ActionColumn', 'template' => '{view} {delete}', 'buttons' => [ 'view' => function ($url, $model) { return ((Yii::$app->user->can("/student/stu/view")) ? Html::a( '<span class="glyphicon glyphicon-eye-open"></span>', $url, ['title' => Yii::t('app', 'View'),] ) : ''); }, 'delete' => function ($url, $model) { return ((Yii::$app->user->can("/student/stu/delete")) ? Html::a( '<span class="glyphicon glyphicon-trash"></span>', false, [ 'class' => 'ajaxDelete', 'delete-url' => $url, 'pjax-container' => 'pjax-list', 'title' => Yii::t('app', 'Delete') ] ) : ''); } ], ], ], ]); ?> <?php \yii\widgets\Pjax::end(); ?> 

Thanks to @skworden for supporting this solution. Link to Yii for this solution. Press here

+1
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'] 

For the notification window you need to make additional material

Full path How to do.

Page 1. Contains a view of the grid.

 <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'layout' => "{pager}\n{summary}\n{items}\n{pager}", 'columns' => [ ['class' => 'yii\grid\CheckboxColumn'], '_id', 'title', 'notification:ntext', 'date', ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {feedback}', 'buttons' => ['feedback' => function ($url, $model, $key) { return Html::a('<i class="glyphicon glyphicon-comment"></i>'.$model->totalfeedback,'#'); }, 'view' => function($url,$model,$key){ return $this->render('_viewLink',['model'=>$model]); }, ], ] ], ]); ?> 

Page 2. This is a Conatin link and Pjax. For each link View, Edit, Delete

 echo Html::a('<span class="glyphicon glyphicon-eye-open"></span>',URL::to(['view','id'=>$model->_id]),['id' => 'view_link']); Pjax::widget(['id'=>'view_member', 'linkSelector' => '#view_link','options'=>['tag'=>'span']]); echo '&nbsp'; echo Html::a('<span class="glyphicon glyphicon-pencil"></span>',URL::to(['update','id'=>$model->_id]),['id' => 'edit_link']); Pjax::widget(['id'=>'view_member', 'linkSelector' => '#edit_link','options'=>['tag'=>'span']]); echo '&nbsp'; echo Html::a('<span class="glyphicon glyphicon-trash"></span>', URL::to(['delete','id'=>$model->_id]), ['id' => 'delete_link']); Pjax::widget(['id'=>'view_member', 'linkSelector' => '#delete_link', 'options'=>['tag'=>'span'],'clientOptions' => ['method' => 'POST']]); 
0
source

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


All Articles