【发布时间】:2014-03-08 11:17:00
【问题描述】:
我正在尝试在通过 ajax 更新的视图中使用 yiibooster select2row 小部件。 我有一个 select2row,其中包含按模型检索的人员姓名列表。
选择一个后,我尝试通过 ajax 和 renderpartial 更新视图中的其他字段。 我这样做是为 on change 事件注册一个脚本。
它第一次工作,但在渲染部分之后它不再工作......甚至小部件也不再正确呈现。
这是我的控制器操作
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 用于渲染部分
$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,以通过 POST 发送 ajax 请求。
我真的不明白为什么它只在第一次起作用.....
谢谢
【问题讨论】:
标签: javascript php ajax yii renderpartial