我有一点问题,当我尝试更新(刷新)div的内容时,Ajax会替换该div中的所有页面。
问题在于:Screenshoot of the problem
这是我的控制器来源:How should it looks
public function actionView() {
//print_r($_POST);
$model = null;
if( "user" == Yii::app()->user->getState("user_type")){
$model = new FormResultUser();
}
else if( "faction" == Yii::app()->user->getState("user_type")){
$model = new FormResultFaction();
}
$index = 0;
//echo $index;
if( Yii::app()->request->isAjaxRequest){
if(isset($_POST['index'])){
$index = CPropertyValue::ensureInteger( $_POST['index'] ) +1;
}
}
//echo $index;
$criteria = new CDbCriteria();
$criteria->limit = "1";
$criteria->offset = $index;
$questionModel = Question::model()->find($criteria);
// print_r($questionModel);
if (Yii::app()->request->isAjaxRequest) {
$this->render('_viewForm', array('model' => $model, 'questionModel'=> $questionModel, 'index'=>$index));
} else {
$this->render('viewForm', array('model' => $model, 'questionModel'=> $questionModel, 'index'=>$index));
}
}
我的viewForm:
在这里你可以看到,AjaxSubmitButton在这里。和<div id="form">
也是,我想要更新。 (我想在ajaxSubmitButton中更新$index
变量)
我也厌倦了'replace'
,但效果不好,问题也一样。
<?php /* FormFilling */ ?>
<div id="form">
<?php $form=$this->beginWidget('CActiveForm'); ?>
<?php
$this->renderPartial('_viewForm', array('model' => $model, 'questionModel'=> $questionModel, 'index'=>$index, 'form'=>$form));
?>
<div class="row margin10">
<?php
echo CHtml::ajaxSubmitButton(Yii::t('strings', 'Next'), Yii::app()->createUrl('formFilling/view'), array(
'type' => 'post',
'data' => array("index" => $index),
'update' => '#form',
));
?>
</div>
<?php $this->endWidget(); ?>
</div><!-- end form -->
<?php
最后我的_viewForm:
<?php
$answers = CHtml::listData(Answer::model()->findAll(), 'value', 'label');
?>
<div id="formAnswer" class="row"></div>
<div class="row text">
<p>
<?php echo CHtml::encode($questionModel->text); ?>
</p>
</div>
<div class="row margin10">
<span>
<?php echo CHtml::encode(Yii::t('strings', 'Answer options'), ''); ?>
</span>
<div class="margin10">
<?php
echo CHtml::activeRadioButtonList($model, 'answer_value', $answers, array("class" => "left"));
?>
</div>
</div>
<div class="row margin10">
<span>
<?php echo CHtml::label(Yii::t('strings', 'Significant topic'), 'ch_answer_important'); ?>
</span>
<?php
echo CHtml::activeCheckBox( $model,
'answer_important',
array("id" => "ch_answer_important", "class" => "left"));
?>
</div>
<?php //echo CHtml::hiddenField('index', $index) ?>
答案 0 :(得分:1)
如果只想在Yii中发送视图模板文件,则需要通过控制器的renderPartial(...)方法渲染视图文件。
在这种特殊情况下,您需要更改此代码
...
if(Yii :: app() - &gt; request-&gt; isAjaxRequest){
$this->render('_viewForm', array('model' => $model, 'questionModel'=> $questionModel, 'index'=>$index));
...
到这个
...
if(Yii :: app() - &gt; request-&gt; isAjaxRequest){
$this->renderPartial('_viewForm', array('model' => $model, 'questionModel'=> $questionModel, 'index'=>$index));
...
就是这样。
答案 1 :(得分:0)
请查看以下YII教程链接到部分内容更新。因此,您只能更新&lt; div&gt;内容。
http://www.yiiframework.com/wiki/49/update-content-in-ajax-with-renderpartial