S
S
Sergey Boldyrev2018-10-09 17:49:07
JavaScript
Sergey Boldyrev, 2018-10-09 17:49:07

Yii2 Pjax - how to send a form in a container to one action, and update the content from another?

I'm trying to make it concise and simple, with built-in tools.
There is such a form.
5bbcbda025a3f009121145.png
A comment should be sent to your controller and the plate will be updated

<?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">Комментарии</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, 'Добавить комментарий') ?>
    <?php ActiveForm::end(); ?>
  </div>
</div>

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

The problem is that the content of the container changes to return from the add comment controller, but should just update the table, like with pjax behavior without a form, can this be configured?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Denis A., 2018-10-09
@phoenix_tf

you can pull the form outside the pjax container, send it with ajax, and reload pjax upon successful save.

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

M
Maxim Timofeev, 2018-10-09
@webinar

It won't work, it's easier to make your bike. Tested by pain, suffering and wasted time. You can use pjax as well, but you can't do without js handles.

S
Sergey Krivosheev, 2018-10-09
@Nemozar

Try to redirect to the form rendering action.

D
DevMan, 2015-07-02
@Dark_Dante

something like this:RewriteRule (.*)/ index.php?$1= [QSA]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question