Dropdown Update

Hi All,

In need of some help here, thanks.

I have an Asset form in which Consumable can be added along with the new Asset record. The Asset and Consumable are 2 separate models. I’m using Joblo’s extension (multimodel) to add Consumable when I’m creating a new Asset record. The Consumable consist of 3 input fields, 1 of the input is a drop-down list of consumable type.

The consumable type drop-down list has an ‘Add New…’ option that allows user to add new consumable type if no matching consumable type is found in the existing drop-down list. I’m using a pop-up dialog from the following extension (CJuiDialog) to add new consumable type.

The problem I’m having happens when I try to add new consumable type to the drop-down list, the newly added consumable type is only reflected in the first drop-down (having multiple consumable drop-down when multiple consumable is added along with the new Asset).

How can I properly update the respective drop-down list in which the ‘Add New…’ is selected from?

The following codes are my implementation,

Asset Form:




$consumableFormConfig = array(

			'elements'=>array(

				'type_id'=>array(

					'type'=>'dropdownlist',

					'prompt'=>'Select',

					'items'=>array( 

						'Type'=>Dropdown::model()->getTypeConsumable()),

					        'onchange'=>"javascript: {

						        if(this.value == 'addNew') {

							      addConsumable();

							      $('#dialogConsumable').dialog('open');

						              return false;

						        }

					        }",

                                         ),

					'quantity'=>array(

						'type'=>'text'

					),

					'expiry_date'=>array(

						'type'=>'zii.widgets.jui.CJuiDatePicker',

						'model'=>$consumable,

						'attribute'=>$consumable->expiry_date,

						'options'=>array(

			                		'dateFormat'=>'yy-mm-dd',

							'changeMonth'=>true,

							'changeYear'=>true,

						),

					),

			));

...

$this->widget('ext.multimodelform.MultiModelForm', array(

		'id'=>'id_consumable',

		'addItemText'=>'Add Consumable',

		'removeConfirm'=>null,

		'removeHtmlOptions'=>array(),

		'formConfig'=>$consumableFormConfig,

		'model'=>$consumable,

		'validatedItems'=>$validatedConsumables,

		'data'=>empty($validatedItems) ? $consumable->findAll(

				array('condition'=>'asset_id=:assetId',

				      'params'=>array(':assetId'=>$model->id),)) : null,

		'tableView'=>true,

		'jsAfterNewId'=>MultiModelForm::afterNewIdDatePicker($consumableFormConfig['elements']['expiry_date']),

								

		'showErrorSummary'=>true,

		'addItemAsButton'=>false,

		'bootstrapLayout'=>true,

		'fieldsetWrapper'=>array('tag'=>'div',

			'htmlOptions'=>array('class'=>'row')),), true),



Javascript:




function addConsumable()

	{		

		<?php echo CHtml::ajax(array(

				'url'=>array('dropdown/create', 'model'=>get_class($consumable)),

				'data'=>"js:$(this).serialize()",

				'type'=>'post',

				'dataType'=>'json',

				'success'=>"function(data)

				{

					if(data.status == 'failure')

					{

						$('#dialogConsumable div.consumableForm').html(data.div);

						$('#dialogConsumable div.consumableForm form').submit(addConsumable);

					}

					else

					{

						$('#Consumable_type_id').prepend('<option value=\"' + data.value[0] + '\">' + data.value[1] + '</option>');							

						$('#Consumable_type_id option[value=\"' + data.value[0] + '\"]').attr('selected', true);


						$('#dialogConsumable div.consumableForm').html(data.div);					

						setTimeout(\"$('#dialogConsumable').dialog('close') \", 1000);							

					}					

				}"

			)) ?>;		

		return false;

	}



Dialog:




<?php 		

	$this->beginWidget('zii.widgets.jui.CJuiDialog', array(

			'id'=>'dialogConsumable',

			'options'=>array(

					'title'=>'Create Consumable Type',

					'autoOpen'=>false,

					'modal'=>true,

					'width'=>580,

					'height'=>420,

			),

	)); ?>

	<div class="consumableForm"></div>

<?php //End dialog 

	$this->endWidget(); ?>



Any help or alternative solution is greatly appreciated. Thanks in advance.

If additional information for the above mentioned problem is needed, please inform me. Thanks.