我的模态窗口中有两个 ActiveForm,提交第一个表单后,我需要更新第二个表单并保持模态。
据我了解 pjax 可以处理这个问题,但无法让它正常工作。
在 _form.php 中,我有 ActiveForm 和应该更新的小部件:
<?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(); ?>
然后我有 Ajax 请求,如果创建新的解决方案,该请求将返回成功:
$.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'});
关闭模态框。
如果我将返回值放在 div 中,则 ajax 可以正常工作并且模式不会关闭。