How to update widget with pjax in modal window in yii2

眉间皱痕 提交于 2019-12-12 07:20:12

问题


I have two ActiveForms in a modal window and after submitting first form, I need to update second one and stay in modal.

As I understand pjax can handle that, but can't get it to work properly.

In _form.php I have ActiveForm with widget wich should be updated:

    <?php $form = ActiveForm::begin([
    'id'=>'form',
    'enableAjaxValidation'=>true,
]); ?>
<?= Html::activeHiddenInput($riskModel, 'id', ['value' => $riskModel->id]) ?>

<?php Pjax::begin([
    'id' => 'solutionItems',
]) ?>
//need to update this widget
    <?= $form->field($riskModel, 'solutions_order')->widget(SortableInput::classname(), [
        'items' => $riskModel->getSolutionList(),
        'hideInput' => false,
        'options' => ['class'=>'form-control', 'readonly'=>false]
    ]); ?>
<?php Pjax::end() ?>

<div class="form-group">
    <?= Html::submitButton($riskModel->isNewRecord ? 'Create' : 'Update', ['class' => $riskModel->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'onclick' => 'return isConnected()']) ?>
</div>

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

And then I have Ajax request wich returns success if new solution is created:

            $.ajax({
            url: form.attr('action'),
            type: 'post',
            data: form.serialize(),
            success: function (data) {
                if (data && data.result == 1) {
                    $.pjax.reload({container:'#solutionItems'});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $("#error").html("Kļūda! Neizdevās pievienot ierakstu.").fadeIn('highlight','', 2000, callbackError());
                $("#solutions-solution").val("");
            }
        });

But

    $.pjax.reload({container:'#solutionItems'});

closes modal :( If I put returned value in div, then ajax works properly and modal is not closing.


回答1:


Managed without $.pjax, just added this

 $("#risks-solutions_order-sortable").append('<li data-id="'+data.id+'" data-key="'+data.id+'" draggable="true">'+data.solution+'</li>');
 $("ul[id$='sortable'").trigger('sortupdate');
 $('#risks-solutions_order-sortable').sortable( "refreshPositions" );

in ajax success and everything is ok! :)




回答2:


Perhaps increasing/disabling timeout may help to solve this problem.

$.pjax.reload('#solutionItems', {timeout : false});

More details you can find here: yii2 how to use pjax when hyperlink is not in pjax



来源:https://stackoverflow.com/questions/28674432/how-to-update-widget-with-pjax-in-modal-window-in-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!