使用ajax调用在局部视图内渲染Yii的Flash消息

时间:2014-01-31 10:05:41

标签: php ajax yii renderpartial

我在Yii中显示Flash消息时遇到问题。在我的视图中,我有一个ajax按钮,调用我的控制器的方法更新。在更新方法中,我想设置一条Flash消息,并在用新数据更新时在我的视图中显示它。

   *update.php :*

    <?php
    <h1>Update Campaign <?php echo $campaign->id; ?></h1>
    <?php
    $tabList = array();
//FORM IS DISPLAYED INSIDE A JUI TAB:
    $tabList['General'] = $this->renderPartial('_form', array('campaign'=>$campaign),true);*

    ...

    $this->widget('zii.widgets.jui.CJuiTabs',array(
        'tabs'=>$tabList,
        'options'=>array(
            'collapsible'=>false,
        ),
    ));
    ?>


*_form.php:*

//HERE I WANT TO DISPLAY A FLASH MESSAGE WHEN _form IS RENDERED:
    <?php foreach(Yii::app()->user->getFlashes() as $key => $message) : ?>
        <div class="flash-<?php echo $key; ?>"><?php echo $message; ?></div>
    <?php endforeach; ?>

    <?php
        Yii::app()->clientScript->registerScript(
            'myHideEffect',
            '$(".flash-success").animate({opacity: 1.0}, 1000).fadeOut("slow");',
            CClientScript::POS_READY
        );
    ?>
    <div class="form">
    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'campaign-form',
        'enableAjaxValidation'=>false,
    )); ?>

        <p class="note">Fields with <span class="required">*</span> are required.</p>

        <?php echo $form->errorSummary($campaign); ?>

        <div class="row">
            <div class="span2">
                    <?php echo $form->labelEx($campaign,'campaign_mode_id'); ?>
            </div>
            <div class="span2">
                <?php echo $form->dropDownList($campaign,'campaign_mode_id', CampaignMode::model()->getModes());?>
                <?php echo $form->error($campaign,'campaign_mode_id'); ?>
            </div>
        </div>
    ...
        <div class="row buttons">
            <div class="span2">
                <?php
                    if($campaign->isNewRecord){
                        echo CHtml::submitButton( 'Create');
                    }else{
//THIS IS MY AJAX-SUBMIT BUTTON, IT CALL CONTROLLER'S UPDATE METHOD AND UPDATE JUI TAB (DIV WITH ID '#yw0_tab0')
                        echo CHtml::ajaxSubmitButton(
                            'Save',
                            Yii::app()->createUrl("//campaign/update/{$campaign->id}"),
                            array('beforeSend' => 'function(){
                                          $("#surveyquestions").addClass("ajaxloading");}',
                                'complete' => 'function(){
                                          $("#surveyquestions").removeClass("ajaxloading");}','update' => "#yw0_tab0"),
                            array('id' => 'send-link-'.uniqid()));
                    }
                ?>
            </div>
        </div>

    <?php $this->endWidget(); ?>


*Contorller:*


    public function actionUpdate($id)
        {
            $campaign=$this->loadModel($id);
            if(isset($_POST['Campaign']))
            {
                $campaign->attributes=$_POST['Campaign'];
                if($campaign->save())
                {
//HERE I'M SETTING THE FLASH MSG
                    Yii::app()->user->setFlash('success','Campaign is updated');

                    if(Yii::app()->request->isAjaxRequest){
//AND UPDATING MY VIEW
                        $this->renderPartial('_form', array('campaign'=>$campaign), true,true);
                    }else{
                        $this->redirect(array('update','id'=>$campaign->id));
                    }
                }
            }
            $this->render('update',array('campaign'=>$campaign));
        }

1 个答案:

答案 0 :(得分:0)

实际上你正在渲染_form视图,但没有将其发送到输出(看看第三个参数),然后你渲染update视图... flash消息已渲染一次在_form中,因此不会再次渲染。

你应该试试这个:

if(Yii::app()->request->isAjaxRequest) {
    $this->renderPartial('_form', array('campaign'=>$campaign), false, true);
    Yii::app()->end();
} else {
    $this->redirect(array('update','id'=>$campaign->id));
}