Yii Framework Forum: How To Populate Dropdownlist - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Populate Dropdownlist

#1 User is offline   venuk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-February 13

Posted 20 November 2013 - 02:02 PM

We did the below to populate a drop down

<code><?php echo $form->dropDownList($model, 'country_id', GxHtml::listDataEx(Country::model()->findAllAttributes(null, true))); ?></code>

But how to do it in Yii2?

I have a state table and country table as in the below
State(id,name, country_id)
country(id,name)

I want to display a country drop down in the state/create form. (I know the way of doing in Yii 1.X version, but I am trying to find in Yii2 style. ) It would be great if that is ajaxdropdown?

Did any body did it already?

Thanks in advance.
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 20 November 2013 - 02:20 PM

You can use new helper ArrayHelper, it's really useful.
For example,

$data = Country::find()
    ->select(['id', 'name'])
    ->asArray()
    ->all();

return \yii\helpers\ArrayHelper::map($data, 'id', 'name');

God is real unless declared as integer
1

#3 User is offline   venuk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-February 13

Posted 21 November 2013 - 04:10 AM

ORey,

Thank you very much for a quick answer. That is working for me.
0

#4 User is offline   Athos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 07-January 11

Posted 13 January 2014 - 07:05 PM

This is the most optimized way to get populate a dropdownlist?
Thanks Google Translator to helping me write the posts :)
0

#5 User is online   zelenin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 24-May 13

Posted 14 January 2014 - 10:52 AM

<?= $form->field( $model, 'source_id')->dropDownList(
ArrayHelper::map( Source::find()->all(), 'id', 'name' ),
[ 'prompt' => '' ]
) ?>

1

#6 User is offline   epulgaron 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 19-February 14

Posted 02 December 2014 - 01:03 PM

how can i emulate the old findallBySql with yii2??
0

#7 User is offline   flarpy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 25-May 10

Posted 04 December 2014 - 08:57 AM

Country::findBySql('SELECT * FROM country')->all();
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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