Answer the question
In order to leave comments, you need to log in
Yii2 PJax why page still reloads?
I read the documentation, but it didn't help. I would like to learn how to use it.
Pjax::begin();
Grid
Pjax::end();
...
'visible' => function($url, $model) {
/**
* @var \app\components\ActiveRecord $model
*/
return GlyphIcon::a('eye-' . ($model->is_visible ? 'open' : 'close'), '',
['visible', 'id' => $model->id, 'state' => $model->is_visible ? 'off' : 'on'],
array_merge_recursive([
'data-pjax' => 'true',
'data-method' => 'post',
], $model->is_visible ? [
'title' => 'Видимый',
'class' => 'icon-actions-active'
] : [
'title' => 'Невидимый',
'class' => 'icon-actions-inactive'
])
);
},
'delete' => function($url, $model) {
/**
* @var \app\components\ActiveRecord $model
*/
return GlyphIcon::a('remove-circle text-alert', '',
['delete', 'id' => $model->id],
[
'title' => 'Удалить',
'data-confirm' => 'Вы действительно хотите удалить эту запись?',
'data-pjax' => '1',
'data-method' => 'post',
]
);
}
...
jQuery(document).pjax("#w0 a", "#w0", {"push":true,"replace":false,"timeout":1000,"scrollTo":false});
jQuery(document).on('submit', "#w0 form[data-pjax]", function (event) {jQuery.pjax.submit(event, '#w0', {"push":true,"replace":false,"timeout":1000,"scrollTo":false});});
Answer the question
In order to leave comments, you need to log in
It is also worth paying attention to the format in which the server returns data, by default pjax waits for html to replace. If the server returns something else (such as an empty document), then the page will simply reload.
I had a similar issue.
If the ajax request went to another action that had a redirect, For example:
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
you need to specify 'format'=>'raw',
www.yiiframework.com/doc-2.0/yii-grid-datacolumn.h...
[
'class'=>'yii\grid\DataColumn',
'attribute'=>'name',
'format'=>'raw',
'value'=>function ($model, $key, $index, $column) {
return a($model->name, ['update','id'=>$model->name], ['data-pjax'=>0]);
},
'contentOptions'=>[
'class'=>'edit-link',
],
],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question