Yii Framework Forum: How to populate a dropDownList using callback? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

How to populate a dropDownList using callback? Rate Topic: -----

#1 User is offline   Armando 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 11-November 08
  • Location:Brazil - Rio de Janeiro

Posted 19 December 2008 - 03:14 PM

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?
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,754
  • Joined: 04-October 08
  • Location:DC, USA

Posted 19 December 2008 - 03:16 PM

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

#3 User is offline   Armando 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 11-November 08
  • Location:Brazil - Rio de Janeiro

Posted 19 December 2008 - 03:27 PM

I will try to do.
thank you qiang :)
0

#4 User is offline   Armando 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 11-November 08
  • Location:Brazil - Rio de Janeiro

Posted 22 December 2008 - 03:17 PM

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?
0

#5 User is offline   jjmf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 10-November 08

Posted 23 December 2008 - 08:42 AM

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.
0

#6 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,754
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 December 2008 - 09:23 AM

Armando: what is your problem?

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

#7 User is offline   Armando 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 11-November 08
  • Location:Brazil - Rio de Janeiro

Posted 23 December 2008 - 12:00 PM

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.
0

#8 User is offline   jjmf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 10-November 08

Posted 23 December 2008 - 01:27 PM

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.
0

#9 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,754
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 December 2008 - 02:03 PM

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

#10 User is offline   jjmf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 10-November 08

Posted 23 December 2008 - 02:44 PM

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.
0

#11 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,754
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 December 2008 - 02:47 PM

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.
0

#12 User is offline   Armando 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 11-November 08
  • Location:Brazil - Rio de Janeiro

Posted 26 December 2008 - 06:31 AM

Thank you Qiang!

The result is very fast! Fantastic!
0

#13 User is offline   arsitek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 10-October 08

Posted 06 March 2009 - 12:07 AM

May you make this as tutorial in cookbook?  :'(

Thanks
0

#14 User is offline   joeysantiago 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 01-April 10

Posted 01 September 2010 - 03:17 AM

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
joey santiago

an Italian with Suomi inside.
0

#15 User is offline   ricardograna 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 375
  • Joined: 31-March 09
  • Location:Manaus/AM - Brazil

Posted 01 September 2010 - 08:09 AM

$related=CHtml::listData(templates::model()->findAll(),'id','nome');
echo CHtml::activeDropDownList($model, 'template_id', $related, array('empty'=>'**Seleziona un layout**'));


Yes, It Is!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users