Yii Framework Forum: dropDownList() and activeDropDownList() - Yii Framework Forum

Jump to content

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

dropDownList() and activeDropDownList() Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 10 November 2009 - 04:44 AM

What is the difference between these two drop down lists?

If I want to use the same drop down list on two different pages, which one is best suited for this?
0

#2 User is offline   datashaman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 27-September 09
  • Location:Cape Town, South Africa

Posted 10 November 2009 - 09:00 AM

View PostGSTAR, on 10 November 2009 - 04:44 AM, said:

What is the difference between these two drop down lists?

If I want to use the same drop down list on two different pages, which one is best suited for this?


They are the same, except that the active one gets its current value from a model.
0

#3 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 10 November 2009 - 11:49 AM

View Postdatashaman, on 10 November 2009 - 09:00 AM, said:

They are the same, except that the active one gets its current value from a model.

When would you need to do that? The current value can always be got by accessing the element in $_GET or $_POST can't it?
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 10 November 2009 - 11:53 AM

When rendering a view.
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 10 November 2009 - 12:11 PM

I don't get it. In my view I just have:

<?php echo CHtml::dropDownList('sort',$_GET['sort'],array(......)); ?>

And that does the job. What benefit does the active method give?
0

#6 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 10 November 2009 - 01:04 PM

View PostGSTAR, on 10 November 2009 - 12:11 PM, said:

I don't get it. In my view I just have:

<?php echo CHtml::dropDownList('sort',$_GET['sort'],array(......)); ?>

And that does the job. What benefit does the active method give?


You get the value from the model in the input field, don't you.
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#7 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 313
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 10 November 2009 - 03:03 PM

The activeDropDownList will initialise the 'selected' value.
Useful mainly for updating an existing record.

For example, if you saved a new record with a country chosen from a list, when editing/updating this record your chosen country would be shown as selected.

I only tend to use the 'active' form elements to be honest.
0

#8 User is offline   ThePaulius 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 17-February 12

Posted 12 April 2012 - 10:46 AM

Wow, this is an old post :lol:

If I use either, i get exactly the same thing.. and BOTH auto select the current value from the DB..

So, to me, it must be a speed thing, becasue cant tell the difference otherwise..

What im stuck with, is that it wont sort the ages.. it shows them in the order they were saved in the database (rather than 0-100)... any ideas?

<?php echo $form->dropDownList($model,'client_1_age', CHtml::listData(FormFields::model()->findAll('id_field=2',array('order' => 'value')), 'value', 'value'), array('empty'=>'Select age')) ?>


<?php echo CHtml::activeDropDownList($model, 'client_1_age', CHtml::listData(FormFields::model()->findAll('id_field=2',array('order' => 'value')), 'value', 'value'), array('empty'=>'Select age')) ?>

0

#9 User is offline   Rom Gapuz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-May 12
  • Location:Philippines

Posted 19 June 2012 - 02:12 AM

View PostThePaulius, on 12 April 2012 - 10:46 AM, said:

Wow, this is an old post :lol:

If I use either, i get exactly the same thing.. and BOTH auto select the current value from the DB..

So, to me, it must be a speed thing, becasue cant tell the difference otherwise..

What im stuck with, is that it wont sort the ages.. it shows them in the order they were saved in the database (rather than 0-100)... any ideas?

<?php echo $form->dropDownList($model,'client_1_age', CHtml::listData(FormFields::model()->findAll('id_field=2',array('order' => 'value')), 'value', 'value'), array('empty'=>'Select age')) ?>


<?php echo CHtml::activeDropDownList($model, 'client_1_age', CHtml::listData(FormFields::model()->findAll('id_field=2',array('order' => 'value')), 'value', 'value'), array('empty'=>'Select age')) ?>



They're the same, if you read above the comparison is between CHtml::dropDownList and CHmtl::activeDropDownList. As datashaman pointed out, the active one gets the value from a model, both $form->dropDownList and CHtml::activeDropDownList gets its values from a model.

I know this $form->dropDownList is the new way to do it.
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