Hi All,
I have a dropdownlist and if element doesn’t exist I create it in a CJuiDialog,
as explained in this wiki
The element doesn’t appear in dropdownlist unless I refresh the page…
How can the dropdownlist be updated without refresh?
If code is needed, controller:
public function actionCreate()
{
$model=new Kategory;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Kategory']))
{
$model->attributes=$_POST['Kategory'];
if($model->save())
{
if (Yii::app()->request->isAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'success',
'div'=>"Kategory added"
));
exit;
}
else
$this->redirect(array('view','id'=>$model->nSchulungsKatID));
}
}
if (Yii::app()->request->isAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'failure',
'div'=>$this->renderPartial('_form', array('model'=>$model), true)));
exit;
}
else
$this->render('create',array(
'model'=>$model,
));
}
_form:
<?php echo CHtml::link('Create kategory', "", // the link for open the dialog
array(
'style'=>'cursor: pointer; text-decoration: underline;',
'onclick'=>"{addKategory(); $('#dialogKategory').dialog('open');}"));?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
'id'=>'dialogKategory',
'options'=>array(
'title'=>'Create Kategory',
'autoOpen'=>false,
'modal'=>true,
'width'=>'auto',
'height'=>'auto',
),
));?>
<div class="divForForm"></div>
<?php $this->endWidget();?>
<script type="text/javascript">
// here is the magic
function addKategory()
{
<?php echo CHtml::ajax(array(
'url'=>array('kategory/create'),
'data'=> "js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#dialogKategory div.divForForm').html(data.div);
// Here is the trick: on submit-> once again this function!
$('#dialogKategory div.divForForm form').submit(addKategory);
}
else
{
$('#dialogKategory div.divForForm').html(data.div);
setTimeout(\"$('#dialogKategory').dialog('close') \",1000);
}
} ",
))?>;
return false;
}
</script>