I’m trying to use the yiibooster select2row widget in a view updated via ajax. I have a select2row with the list of name of people retrieved by model.
After selecting one I try to update the other fields in the view by ajax and renderpartial. I do that registeriing a script for the on change event.
It works the 1st time, but after the renderpartial it don’t work anymore … and even the widget is not rendered correctly anymore.
Here is my controller action
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,));
}
}
Here my view
// 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');
And here my _form.php used in 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();
In the form I used an hidden field to store the id used in the script to send the ajax request via POST.
Really I don’t understand why it works only the first time …
Thanks