我正在尝试在通过ajax更新的视图中使用yiibooster select2row小部件。我有一个Select2row,上面有模型检索的人名称列表。
选择一个后,我尝试通过Ajax和RenderPartial更新视图中的其他字段。我要为ON变更事件注册脚本。
它是第一次工作,但是在渲染方上它不再起作用了……甚至小部件也无法正确渲染。
这是我的控制器动作
public function ActionView() {
// Model Setting
if(isset($_POST[idKey]) and $_POST[idKey] > 0) {
$docente=Docente::model()->findByPk($_POST[idKey]);
} else {
$docente=new Docente();
}
if (Yii::app()->request->isAjaxRequest) {
echo CJSON::encode(array(
'divFormScheda'=>$this->renderPartial('_form',array('docente'=>$docente,), true, false),
));
} else {
$this->render('view', array('docente'=>$docente,));
}
}
在这里我的视图
// Script to make the ajax update on change of the select2 widget
Yii::app()->clientScript->registerScript('wric', "
jQuery('#Docente_id').on('change', function(e) {
var idKey = $('#Docente_id').val();
jQuery.ajax({
'url':'/regele/docente/view',
'data':$(this).serialize() + '&idKey=' + idKey,
'type':'post',
'dataType':'json',
'success':function(data) {
$('#divFormScheda').html(data.divFormScheda);
},
'cache':false
});
return false;
});
");
//
// Display Scheda
//
echo CHtml::openTag('div',array('id'=>'divFormScheda'));
$this->renderPartial('_form', array('docente'=>$docente,), false, false);
echo CHtml::closeTag('div');
和我的_form.php在renderpartial
$form = $this->beginWidget(
'bootstrap.widgets.TbActiveForm',
array(
'id' => 'idFormR',
'type' => 'horizontal',
'htmlOptions' => array('class' => 'well'),
)
);
// Error
echo $form->errorSummary($docente,"Errori riscontrati:<br /><br />");
// Input Hidden to read the idKey to set the ajax post
echo $form->hiddenField($docente, 'id', array('id' => 'idKey'));
// select2row widget for the selection
echo $form->select2Row($docente, 'id',
array(
'data' => CHtml::listData(Docente::model()->findAll(), 'id', 'cognome'),
'options' => array(
'placeholder' => 'Select ...',
'allowClear' => true,
'width' => '40%',
)
)
);
// Fields
echo $form->textFieldRow($docente, 'nome', array('class' => 'input-xlarge', 'disabled' => true));
$this->endWidget();
在形式中,我使用隐藏字段存储脚本中使用的ID,以通过帖子发送AJAX请求。
真的我不明白为什么它只有第一次工作....
谢谢
您应该将processOutput设置为true即:
$this->renderPartial('_form',array('docente'=>$docente,), true, true);
它应该以这种方式工作