E
E
Evgeny Kylin2017-04-27 08:50:29
Yii
Evgeny Kylin, 2017-04-27 08:50:29

How to initialize Pjax in Yii2?

Data is loaded into the modal form via Ajax:

$('[firmmodal]').on('click',function(){
    console.log('s');
    $.ajax({
      type: 'GET',
      url: '/firm/update/',
      data: {
        id: $(this).attr('firmmodal')
      },
      success: function(data){
        $('#firmModalUpdate').html(data);
      }
    })
  })

These ones:
<?php Pjax::begin(['enablePushState' => false, 'id' => 'firmUpdate']); ?>
      <?php $form = ActiveForm::begin(['options' => ['data-pjax' => true]]); ?>
.......
<?= Html::submitButton($model->isNewRecord ? 'Создать' : 'Обновить', ['class' => 'uk-button uk-button-primary', 'data-pjax' => '1']) ?>
    <?php ActiveForm::end(); ?>
  <?php Pjax::end(); ?>

When submitting the form, the page reloads. I sin that Pjax is not initialized when loading via Ajax, and accordingly it is not sent.
Here is the controller:
public function actionUpdate($id = false)
    {
    	if($id){
      $model = $this->findModel($id);
    }else{
      $model = new Firm();
    }
    $this->layout = false;
    $model->_save = false;
    if ($model->load(Yii::$app->request->post()) && $model->save()) {
      $model->_save = true;
    }
    return $this->render('update', [
            'model' => $model,
        ]);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mi7teR, 2017-04-27
@Mi7teR

www.yiiframework.com/doc-2.0/yii-widgets-pjaxasset.html
asset is present on the page where pjax connects?

M
Maxim Timofeev, 2017-04-27
@webinar

I don't understand why there is ajax. Is it not possible to do this through the same pjax?
But I would check where the pjax request is knocking and if there is an error there, I would also increase the timeout, if pjax does not receive a response within timeout (1000ms), it throws a regular get. Maybe that's the thing. Put to check:

Pjax::begin([
'timeout'=>60000
])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question