php - Updating Yii Dropdownlist After Insert New Element [SOLVED] -
i'm totally new in yii, so, excuse me if answer question little bit trivial.
here's 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 button register new phone types. so, after submition of form, inside of cjuidialog, wish above dropdownlist updated new type, without refresh page.
i google lot, find things related "dependent dropdowns" in yii.
what's better approach solve problem? there $.fn.cgridview.update?
here's 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 code of controller, trivial create function:
public function actioncreate(){ $model = new phonetype; if(isset($_post['phonetype'])){ $model->attributes = $_post['phonetype']; if( $model->save() ){ //----> suggestion here? echo chtml::script(""); yii::app()->end(); } } } so, below code of solution. in 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 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, )); } thanks help!
you have action adds phone type (for example, let's call phonetype/create).
when send ajax request action create phone type, action should return newly created phone types information. can add dropdown using jquery.
look @ example:
<?php // in protected/controllers/phonetypecontroller.php public function actioncreate($phonetype) { $phonetype = new phonetype; $phonetype->phone_type = $phonetype; if ($phonetype->save()) { echo cjson::encode(array('value' => $phonetype->id, 'label' => $phonetype->phone_type)); // echos {"value":5,"label":"test"} } } ?> for rest of example, i'm going assume original model (the 1 have phone_type field) called company (this has impact on jquery code below i'm selecting phone_type dropdown).
you can use output in success callback of ajax function add new option select (i.e. dropdown):
jquery.get( // 'data' , 'url' here success: function(data) { $('#company_phone_type').append('<option value="' + data.value + '">' + data.label + '</option>'); }); for more information on how in jquery, refer adding options select using jquery/javascript.
Comments
Post a Comment