[英]How to update widget with pjax in modal window in yii2
我在模態窗口中有兩個 ActiveForms,在提交第一個表單后,我需要更新第二個並保持模態。
據我了解,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("");
}
});
但
$.pjax.reload({container:'#solutionItems'});
關閉模態。 如果我將返回值放在 div 中,則 ajax 可以正常工作並且模式不會關閉。
沒有 $.pjax 管理,只是添加了這個
$("#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 成功,一切正常! :)
也許增加/禁用超時可能有助於解決這個問題。
$.pjax.reload('#solutionItems', {timeout : false});
您可以在此處找到更多詳細信息: yii2 如何在 pjax 中沒有超鏈接時使用 pjax
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.