【问题标题】:How to refresh Yii widget without refreshing/changing page如何在不刷新/更改页面的情况下刷新 Yii 小部件
【发布时间】:2015-03-03 10:16:54
【问题描述】:

我在提交 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') 是一起更改页面网址。在我被定向到的这个页面上,视图创建得很好。我需要小部件在当前页面上重建/刷新,而不是将我发送到其他地方。任何有关解决方案的想法将不胜感激。

【问题讨论】:

    标签: javascript php jquery yii


    【解决方案1】:

    我会这样做:

    1. 将表单小部件包装在 div 或任何你喜欢的块标签中。<div id="myFormWrapper"> (your widget goes here) </div>
    2. 在表单中添加自定义 ID (id="formId") 并提交按钮 (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');
              }
           }
        }
    
    

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 2011-09-01
      • 2017-05-11
      • 2019-11-25
      • 2017-12-21
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多