Yii Depedent Dropdownlist With Pre-Selected Values Passed From The Controller Not Working

I have several dropdownlist which are working like charm except when there are preselect values passed from the controller to the form so much so that the form will be render with the preselected values in each dropdown. This how i have passed the variables in the controller




   public function actionBodyTrim(){

    $trim = new CarTrim;

    $spares = new Spares;


    $spares->makeid  = $_REQUEST['mid'];

    $spares->modelid = $_REQUEST['moid'];

    $spares->bodyid  = $_REQUEST['bid'];

    $spares->trimid  = $_REQUEST['tid'];


    $this->render('bodytrim', array(

                  'trim'=>$trim,

                  'spares'=>$spares

                  )

    );

}

When the form is rendered the first dropDownList is displaying the pre-selected value from the controller. But all the other dropDownlist are not working. Actually on the view the second dropDownList show the raw Ajax code, meaning the Ajax is not firing correctly. The result is shown below




<div class="span5 leftpull">

    <div class="row">

     <div class="width125"><?php echo $form->labelEx($model,'makeid'); ?></div>

     <div class="widthtxt"><?php echo $form->dropDownList($model,'makeid', CHtml::listData(Makes::model()->findAll(array('order' => 'makename ASC')), 'makeid', 'makename'),

                                     array('prompt'=>'Select Makes',

                                           'ajax'=>array('url'=>CController::createUrl('CarModels'),

                                            'beforeSend' => 'function(){$("#carmodels").addClass("loading");}',

                                            'complete' => 'function(){$("#carmodels").removeClass("loading");}',

                                           'type' =>'POST',

                                           'update'=>'#'. CHtml::activeId($model, 'modelid'),

                                           array('class'=>'ajaxlink'),


                                        ))); ?>




                <?php echo $form->error($model,'makeid'); ?>

      </div>

</div>


<div class="row" id="carmodels">

     <div class="width125"><?php echo $form->labelEx($model,'modelid'); ?></div>

     <div class="widthtxt"><?php echo $form->dropDownList($model, 'modelid', empty($model->modelid) ? array('prompt' => 'Select the country first') :

                                      array('ajax'=>array('type'=>'POST',

                                                          'url'=>CController::createUrl('BodyType'),

                                                          'update'=>'#'. CHtml::activeId($model, 'bodyid'),

                                                          'prompt'=>'Models',

                                                          array('class'=>'ajaxlink'),


                                               ))); 

        ?>

        <?php echo $form->error($model,'modelid'); ?>

    </div>

</div>



Below is the HMTL code of the form with the Ajax function showing as values of the dropdown not firing as required.




<div class="row" id="carmodels">

         <div class="width125"><label for="Spares_modelid" class="required">Model <span class="required">*</span></label></div>

         <div class="widthtxt"><select name="Spares[modelid]" id="Spares_modelid">

<optgroup label="ajax">

<option value="type">POST</option>

<option value="url">/spareparts/index.php?r=spares/parts/BodyType</option>

<option value="update">#Spares_bodyid</option>

<option value="prompt">Models</option>

<optgroup label="0">

<option value="class">ajaxlink</option>

</optgroup>

</optgroup>

</select>                   </div>

    </div>



I know the problem is more likely on the view but i can’t point my finger on the issue. I appreciate you response in advance.

I think you may use this onchange event using jquery on second dropdown list.

for e.g




 'onchange' => 'geteventlist(this.value)',


<script>

 function geteventlist(venue_id) {

    	$.ajax({

        	type: 'POST',

        	url: '<?php echo CController::createUrl('support/eventname'); ?>',

        	async: false,

        	data: {"venue_id": +venue_id},

        	success: function(data) {

            	$('#date_id_report').html('');

            	$('#event_name_report').html(data);

            	return false;

        	}

    	});

	}

</script>

Hi Ankit,

i have tried your example and on the second dropdown as you had suggested i am getting this geteventlist(venue_id) as the value. Could you paste your view i see.

Thanks

Anthony

i have managed to make it work with a dirty work-round using (if statements ). I just hope there is someone out there with a cleaner code than this.




  <?php

		 if (isset($model->makeid) && !isset($model->modelid)  ) 

		 {     		

				$list = CHtml::listData(CarModels::model()->findAll('makeid=:mid',

														    array(':mid'=>$model->makeid)), 'modelid', 'model');

				echo $form->dropDownList($model,'modelid', $list, 

									      array( 'prompt'=>'Select Models',

												 'ajax'=>array('type'=>'POST',

															  'url'=>CController::createUrl('BodyType'),

															  'update'=>'#'. CHtml::activeId($model, 'bodyid'),

															  

						 ))); 

				

		 }

		 

		 else (isset($model->makeid) && isset($model->modelid) ){

			  $list = CHtml::listData(CarModels::model()->findAll('makeid=:mid  AND  modelid=:mod',

														    array(':mid'=>$model->makeid, ':mod'=>$model->modelid )), 'modelid', 'model');

				

		        echo $form->dropDownList($model,'modelid', $list, array('prompt'=>'Select Models'));

			  

		  }

?>