如何在yii2的模式窗口中使用pjax更新小部件 [英] How to update widget with pjax in modal window in yii2
问题描述
我在模态窗口中有两个ActiveForm,提交第一个表单后,我需要更新第二个表单并保持模态。
I have two ActiveForms in a modal window and after submitting first form, I need to update second one and stay in modal.
据我了解,pjax可以处理此问题,但无法解决可以正常工作。
As I understand pjax can handle that, but can't get it to work properly.
在_form.php中,我的ActiveForm带有小部件,应该进行更新:
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(); ?>
然后,如果创建了新的解决方案,那么我有Ajax请求,如果返回成功,则返回:
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("");
}
});
但是
$.pjax.reload({container:'#solutionItems'});
关闭模式:(
如果我将返回值放在div中,那么ajax可以正常工作,并且
closes modal :( If I put returned value in div, then ajax works properly and modal is not closing.
推荐答案
在没有$ .pjax的情况下进行管理,只需添加此
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" );
在ajax成功中,一切都OK!:)
in ajax success and everything is ok! :)
这篇关于如何在yii2的模式窗口中使用pjax更新小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!