Dynamic Dependant Drop-Down Lists

Hi,

I’ve implemented a cascade of dropdown lists that work fine in the case you arrive at the form with all lists at their default position.

However, in another use case the first list is pre-selected to a certain value to save the user time.

In this circumstance the list has to be changed, and then changed back to the value required to get the dependant list to load, which doesn’t save time! Is there someway to fire off the dependant list population when the list is loaded as well as changed?

Code from form view:




        <?php           echo CHtml::dropDownList('winery_id',$winid,$wineries, 

			array('empty' => 'Select winery',

			'ajax' => array(

			'type'=>'POST', //request type

			'url'=>CController::createUrl('Labels/DynamicLabels'), //url to call.

			'update'=>'#label_id', //selector to update

			))); 

	 ?>



Part of the answer is I’d be better with a Typeahead, but still working on that.

Any pointers much appreciated.

cheers

IF I understood your question in correct way , " data return to zero after submit ? "

So solution will be @ this link

http://www.yiiframework.com/forum/index.php/topic/38335-load-data-by-ajax-disappeared-when-valdiation-return-erorr/page__p__184376__fromsearch__1#entry184376

Thanks for the response, but that’s not my issue. The drop downs work fine. I’m really trying to make them do something more.

Of course, just fire the change event after the page loads.


$(function() {

  $('#winery_id').trigger('change');

});

I have the same issue with my dropdowns and @tsunami’s idea didn’t work. Any other ideas are appreciated.

It works for me, so if you show your code someone can tell you what you’re doing wrong.

Perhaps I don’t know where to insert your code. My _form.php looks like this:




<?php  echo $form->dropDownListRow($modelVehmake,'id',$this->FetchActiveVehmakes,array(

	'prompt'=>'(Select Make)',

	'ajax'=>array(

		'type'=>'POST',

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

		'update'=>'#Vehmodel_id',

		),

	));

?>

<?php  echo $form->dropDownListRow($modelVehmodel,'id',$listvehmodels,array(

	'ajax'=>array(

		'type'=>'POST',

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

		'update'=>'#trimcode',

		),

	));

?>

<?php echo $form->dropDownListRow($model,'vehtrim_id',$listvehtrims,array(

		'id'=>'trimcode',

	));

?>



The first dropdown fetches vehicle makes directly and uses Ajax to fill the second dropdown which presents the vehicle models. The problem is that if the 1st loaded vehicle model has one or more trims (engine size, color, etc.) already defined they aren’t Ajax loaded into the 3rd dropdown - one has to manually change the vehicle model to something else and then go back to trigger the 3rd dropdown to update. I’m hoping that some kind of javascript “onchange” code will accomplish this.

Any ideas are greatly welcome.

Thanks for the suggestion, but I ended up testing if the ID of the first dropdown was set, and then loading the array of the second one:




    if($winid==-1)

    {

        $labels=array();

    }

 else 

    {

    $labels=CHtml::listData(Wineries::model()->getLabels($winid),'id','name');

    }



and then the second drop down uses:




echo CHtml::dropDownList('label_id','', $labels,

			array('empty' => 'Select Label',



The variable $winid is either set to -1 if no winery is pre-selected, or the winery ID if it is, so not as elegant as triggering the existing function, but works just the same.

This sounds perfect - but I’ve no idea where that code would go.

It’s JavaScript, so between <script> tags somewhere in your HTML.