Yii2 Pjax — like shape in a container to send to one action, and update your content from another?

Trying to make succinctly and simply, and integrated.
There is a form
5bbcbda025a3f009121145.png
Must go comment on your controller and updated the sign
<?php Pjax::begin([
 'id' => 'comments-pjax',
 'enablePushState' => false,
 'formSelector' => '#comments-form'
]); ?>

<div class="box box-primary">
 <div class="box-header with-border">
 <h3 class="box-title">Comments</h3>
</div>
 <div class="box-body">

 <?= GridView::widget([
...
 ]); ?>

</div>
 <div class="box-footer">
 <?php $form = ActiveForm::begin([
 'id' => 'comments-form',
 'action' => '/lead-comments/create?lead_id='.$model->id,
 ]); $comm = new LeadComments; ?>
 <?= $form->field($comm, 'lead_id')->hiddenInput(['value' => $model->id])->label(false) ?>
 <?= $form->field($comm, 'text')->textarea() ?>
 <?= app\components\ViewHelper::saveButton(null, 'Add review') ?>
 <?php ActiveForm::end(); ?>
</div>
</div>

<?php Pjax::end(); ?>

The problem is that the contents of the container is changed to return from a controller add a comment, and just need to update the table, as when the behavior of pjax without form, whether it is possible so to adjust?
March 12th 20 at 08:00
3 answers
March 12th 20 at 08:02
Solution
you can get a form outside the pjax container, send it to Ajax and pjax to reloadit at successful saving.

$(document).on('beforeSubmit', '#my-form', function () {
 var _this = $(this);

$.ajax({
 url: _this.attr('action'),
 data: _this.serialize(),
 type: 'POST',
 dataType: 'json',
 success: function (response) {
 if (response.success) {
$.pjax.reload({
 container: "#my-pjax-container-id" 
});
 } else {
 // alert
}
},
 error: function (jqXHR, textStatus, errorThrown) {
 console.info(textStatus + '' + errorThrown);
}
});

 return false;
 });
Yes, in General, exactly the first did, but was faced with the problem of zagublena request to the controller, and decided to try a more succinct at first glance, option.
Eventually went back to this option and did just that, dubbing the query somehow by itself disappeared (probably due to using the beforeSubmit submit is not prompt, by the way, why so?), so in General this decision is probably the most correct. - felton commented on March 12th 20 at 08:05
March 12th 20 at 08:04
It does not, it is easier to make your bike. Checked pain, suffering and wasted time. Using pjax including, but without js handles is not enough.
March 12th 20 at 08:06
Try to make a redirect to action of form rendering.

Find more questions by tags YiijQuery