I am using Pjax with Gridview and I want all my action button do ajax. By default, they dont, so I googled and found way to remove data-pjax = 0. But still , there are no ajax requests, all of them are regular requests.
Lots people are having this problem and I couldnt find the solution as well.
I've followed:
My code:
<?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(); ?>
Have anyone found the solution for this issue yet ?
Try this. ( my working code )
First register following JavaScript
on above Gridview
views file
Here i'm uses the bootbox confirm box.
$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 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 for this solution. Yii forum link for this solution. click here