Here's my code:
<p>
<?php echo $form->labelEx($model,'phone_type'); ?>
<span class="field">
<?php echo $form->dropDownList($model,'phone_type',
CHtml::listData(PhonesTypes::model()->findAll(),
'id','type' )); ?>
<?php echo $form->error($model,'phone_type'); ?>
</span>
</p>
There will be a button to register new Phone types. So, after the submition of the form, that will be inside of a CJUiDialog, I wish that the above dropDownList be updated with the new type, without refresh the page.
I google it a lot, but i only find things related to "dependent dropdowns" in Yii.
What's the better approach to solve this problem? Is there something like $.fn.cgridview.update?
Here's the Dialog code:
<?php $this->endWidget('zii.widgets.jui.CJuiDialog');
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'dialog-crud',
'options'=>array(
'title'=>'Create new Phone Type',
'autoOpen'=>false,
'modal'=>true,
'width'=>1080,
'height'=>820,
'resizable'=>false
),
));
?>
<iframe src="http://myapp/phone_types/create" width="100%" height="100%"></iframe>
<?php $this->endWidget(); ?>
And the code of the controller, is a trivial create function:
public function actionCreate(){
$model = new PhoneType;
if(isset($_POST['PhoneType'])){
$model->attributes = $_POST['PhoneType'];
if( $model->save() ){
//----> some suggestion here? echo CHtml::script("");
Yii::app()->end();
}
}
}
So, below is the code of my solution. In the view:
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'dialog',
'options'=>array(
'title'=>'Phone Types',
'autoOpen'=>false,
'modal'=>true,
'width'=>1080,
'height'=>820,
'resizable'=>false
),
));
?>
<iframe src="phoneTypes/create" id="cru-frame" width="100%" height="100%"></iframe>
<?php $this->endWidget(); ?>
In my PhoneTypesController:
public function actionCreate(){
$model = new PhoneTypes;
if(isset($_POST['PhoneTypes'])){
$model->attributes = $_POST['PhoneTypes'];
if($model->save()){
echo CHtml::script("
window.parent.$('#dialog').dialog('close');
window.parent.$('#Phone_types_id').append('<option value=".$model->id." >'+'".$model->type."'+'</option>');
");
Yii::app()->end();
}
}
$this->render('create',array(
'model'=>$model,
));
}