E
E
EvilDev2014-07-05 13:52:11
Yii
EvilDev, 2014-07-05 13:52:11

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();

I create buttons like this, for example, here are some of them in the ActionColumn:
...
'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',
    ]
  );
}
...

The code that Yii generates
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});});

Only data-confirm works, in all other cases it reloads the page. renderAjax in the controller doesn't help either. What am I doing wrong? Thanks for the replies.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
ILYA, 2015-04-27
@iliacmd

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.

R
rabadan731, 2015-03-16
@rabadan731

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

then the page was reloaded.
If you remove the redirect, everything works as it should.

Z
Zohei, 2014-11-03
@Zohei

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',
            ],
        ],

R
Ruslan, 2015-07-21
@mitrm

Need to increase timeout

A
Anton, 2015-04-21
@karminski

Pjax is hindered by this attribute: 'data-method' => 'post'. Remove it and everything will be ok.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question