我在提交 TbActiveForm 时遇到问题,而 renderPartial 会删除我的页面并仅显示部分 View 。我只想在操作触发并完成后重新加载小部件。我还使用模式来显示和进行更改。
查看:
$form = $this->beginWidget(
'booster.widgets.TbActiveForm',
array(
'id' => 'horizontalForm',
'type' => 'horizontal',
'action' => Yii::app()->createUrl('orderControl/order/returns.save'),
)
);
echo $form->hiddenField(
$editReturnFormModel,
'orderId',
array(
'value' => $editReturnFormModel->orderId
)
);
$this->widget(
'bootstrap.widgets.TbButton',
array('buttonType' => 'submit', 'type' => 'primary', 'label' => 'Save')
);
$this->endWidget();
行动:
$this->controller->renderPartial('ScModules.orderControl.widgets.ReturnsWidget.views._returnItems', array('returnsDataProvider'=>$returnsDataProvider, 'editReturnFormModel'=>$editReturnFormModel));
另一点是 Yii::app()->createUrl('orderControl/order/returns.save')
一起更改页面 url。在我定向到的这个页面上, View 创建得很好。我需要小部件在当前页面上重建/刷新,而不是将我发送到其他地方。任何有关解决方案的想法将不胜感激。
最佳答案
这就是我要做的:
- 将表单小部件包裹在 div 或您喜欢的任何 block 标记内。
<div id="myFormWrapper"> (your widget goes here) </div>
- 在表单中添加自定义 ID (id="formId") 和提交按钮 (id="submitButtonId")
- 添加一些 jQuery 以提交表单并用新的小部件替换旧的小部件
$(document).on('click', '#submitButtonId' , function() {
$.ajax({
type: 'POST',
url: $('#formId').attr('action'),
data : $('#formId').serialize(),
beforeSend : function(){
//do anything you want before sending the form
},
success : function(data){
//We'll replace the old form with the new form widget
$('#myFormWrapper').html(data);
},
error : function(data){
console.log('ops');
},
});
return false;
});
- 在 Controller 操作中执行您想做的任何操作并使用 renderPartial。
//The action below should be the same that you used in the action attribute of the form
public function actionProcessForm(){
$model = new MyFormModelName();
if(isset($_POST['MyFormModelName'])){
$model->attributes = $_POST['MyFormModelName'];
if($model->save()){
//Do whatever you want here
$returnsDataProvider = new CActiveDataProvider('YourModel');
$this->renderPartial('//folder/to/your/view', array('returnsDataProvider'=> $returnsDataProvider, 'editReturnFormModel'=>$editReturnFormModel));
}else{
//You might want to render something else here
$this->renderPartial('errorViewPage');
}
}
}
关于javascript - 如何在不刷新/更改页面的情况下刷新 Yii 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28748823/