Kri*_*ode 5 php ajax pjax yii2 active-form
我在一个模态窗口中有两个ActiveForms,在提交第一个表单后,我需要更新第二个表单并保持模态.
据我所知,pjax可以处理,但无法让它正常工作.
在_form.php中,我有ActiveForm的widget应该更新:
<?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(); ?>
Run Code Online (Sandbox Code Playgroud)
然后我有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("");
}
});
Run Code Online (Sandbox Code Playgroud)
但
$.pjax.reload({container:'#solutionItems'});
Run Code Online (Sandbox Code Playgroud)
关闭模态:(如果我把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" );
Run Code Online (Sandbox Code Playgroud)
在ajax中成功,一切都好!:)