繁体   English   中英

如何在不刷新/更改页面的情况下刷新Yii小部件

[英]How to refresh Yii widget without refreshing/changing page

我在提交TbActiveForm和renderPartial擦掉页面并仅显示部分视图时遇到问题。 我只想在动作触发并完成后重新加载小部件。 我还使用模式显示和进行更改。

视图:

$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')一起更改了页面网址。 在此页面上,我创建了该视图。 我需要在当前页面上重建/刷新小部件,而不要将我发送到其他地方。 任何解决方案的想法将不胜感激。

这就是我要做的:

  1. 将表单小部件包装在div或所需的任何块标签中。 <div id="myFormWrapper"> (your widget goes here) </div>
  2. 在您的表单(id =“ formId”)中添加一个自定义ID,然后提交按钮(id =“ submitButtonId”)
  3. 添加一些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;
    });

  1. 做任何您想在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');
          }
       }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM