duangai2831
duangai2831
2014-01-31 10:05

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

  • yii
  • php
  • ajax

I have a problem displaying Flash messages in Yii. Inside my view I have an ajax button, calling method update of my controller. Inside the update method I want to set a Flash message and display it inside my view when it's updated with new data.

   *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条回答