How to populate a dropDownList using callback?

I’m using  ajaxDropDownList ;D with callback… and I already have the result of the search in a array.

Now, how to populate another dropDownList?

In PRADO I use this:



$series = $this->getSeriesByIdCurso($idCurso);


$this->DropSerie->setDataSource($series);


$this->DropSerie->dataBind();


In Yii, the dropDownList should be active?

Someone help me?

In Yii, you use CHtml::activeDropDownList and CHtml::listData.

I will try to do.

thank you qiang :)

I'm trying the following way:



<?php echo CHtml::dropDownList('curso', '', CHtml::listData($curso, 'id_curso', 'nome'), array('style'=>'width:200px', 'prompt'=>'Selecione...', 'ajax'=>array('type'=>'POST', 'replace'=>'id_serie'))) ?>





<?php echo CHtml::activeDropDownList($serie, 'id_serie', CHtml::listData($serieList, 'id_serie', 'nome'), $htmlOptions = array('multiple'=>'multiple', 'style'=>'width:200px; height:60px')); ?>


In EventosController.php I have:

 


public function actionCalendario() { // index





if(Yii::app()->request->isAjaxRequest){


         $this->actionCarregaSerie();


      }





   ....





   $serie = new serie;


   $serieList = array(); // empty array on first view.





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


         'curso'=>$curso,


         'tipoeventoList'=>$tipoeventoList,


         'avaliacaoList'=>$avaliacaoList,


         'turnoList'=>$turnoList,





         'serie'=>$serie,


         'serieList'=>$serieList,


         ));


}








   public function actionCarregaSerie()


   {


      $serie = new serie;


      $serie->id_curso = $_POST['curso'];


      


      $sql = "SELECT * FROM `serie` WHERE id_curso = $serie->id_curso";


      $serieList = serie::model()->findBySql($sql);


   }


Is there some wrong thing in my code?

I´m also trying to do this, but no success.

I want to populate a dropdownlist according to the selection in another dropdownlist. For example:

In my Controller:

public function actiondrop()


	{


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


			'countries' => $this->dataCountry,


			'states' => array(),


		));


	}


	


	


	public function actionloadStates()


	{


		$return = '';


		foreach ($this->dataStates[$_POST['country']] as $idState => $state){


			$return .= "<option value='{$idState}'>{$state}</option>";


		}


		echo $return;


	}		

In my view (drop.php):

<?php echo CHtml::form(); ?>





<?php


echo CHtml::dropDownList('country', '', $countries, array( //htmloptions


	'ajax' => array(


		'type'=>'POST',


		'url'=>'index.php?r=site/loadStates',


		'update'=>'state',


	),


));


?>


<br/>


<br/>





<?php echo CHtml::dropDownList('state', '', $states); ?>





<?php echo CHtml::closeTag('form'); ?>

I'm confused. I don't know what the actionloadStates() should return.

Armando: what is your problem?

jjmf: Please use firebug to check what is going on with your ajax response.

I thought I was writing something absurdity and could leave someone confuse. was that?

Me and JJMF, we have the same problem to render the dropdownlist.  we are of the same department here in the company.

We are trying to change our system in PRADO to Yii.

This is what is show in the firebug response tab when I select 'Brazil' as the country:

<option value='0'>Rio de Janeiro</option><option value='1'>Brasilia</option><option value='2'>Sao Paulo</option>

And this is what is show when I select USA:

<option value='0'>Florida</option><option value='1'>New York</option>

That is correct but these options doesn't appear in the states dropdownlist.

Change 'update'=>'state' to 'update'=>'#state' because you are using id as an element selector.

Great qiang! Thanks, it worked!

Is this supposed to really work this way?

Or are there any other way to accomplish this task?

I mean, in prado we just get an array of activerecord objects and databind it to the desired dropdownlist.

In Yii I have to gererate the html code and echo it?

Another question: Yii sholdn't place the '#' char automatically? Or do I have to always place is by my self?

Thanks for your help.

Yes, the way you are doing is correct.

Of course, if in future we have some more intelligent dropdown widget, you may no longer need to write raw HTMLs. For now, this is the way to go.

Also, it is correct that you should use '#state' because the 'update' value could be anything as long as it is a valid jquery selector.

Thank you Qiang!

The result is very fast! Fantastic!

May you make this as tutorial in cookbook?  :cry:

Thanks

sorry, guys… i’m trying to do something like you did, but can’t succeed.

Can’t understand the functions you add in your controller file. what’s the theory behind names and return values?

this is my activedropdownlist:


$related=CHtml::listData(layouts::model()->findAll(),'id','nome');

		echo CHtml::activeDropDownList(

                    $model,

                    "layout_id",

                    $related,

                    array(

                        'empty'=>'**Seleziona un layout**',

                        'ajax'=>array(

                            'type'=>'GET',

                            'replace'=>'#campagne_template_id'

                        )

                    )

                );

i’d like it to populate the template_id activeDropDownList with data coming from templates model : id as value and name as label.

Can you please help me out?

thanks a lot


$related=CHtml::listData(templates::model()->findAll(),'id','nome');

echo CHtml::activeDropDownList($model, 'template_id', $related, array('empty'=>'**Seleziona un layout**'));