Yii Framework Forum: Duplicate options in dropDownList - Yii Framework Forum

Jump to content

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

Duplicate options in dropDownList Rate Topic: -----

#1 User is offline   andyofb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 24-August 10

Posted 06 May 2011 - 06:13 AM

I want to have a dropdown list with

United Kingdom

Country 1
Country 2
Country 3
Country 4
Country 5
Country 6
Country 7
Country 8
United Kingdom
Country 9
Country 10
Country 11

How do I achieve this as United kingdom has the same value in the option (therefore I cant pass an array with the same key twice)

Thanks
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,075
  • Joined: 16-February 11
  • Location:Japan

Posted 06 May 2011 - 09:46 AM

It's impossible with an ordinary 1 level array like this.
$countries = array(
    1 => 'country 1',
    2 => 'United Kingdom',
    3 => 'country 3',
    ....
    7 => 'country 7',
    2 => 'United Kingdom',
    8 => 'country 8,
    ....
);
echo CHtml::dorpDownList('country', $country_id, $countries);

The 2nd one will just overwrite the 1st one, and no duplicate entries will be created.
You have to use another key for the 2nd entry while you may use the same label for it.

So, how about this?
Using a 2 level array, it will group the items.
$countries = array(
    'category 1' => array(
        1 => 'country 1',
        2 => 'United Kingdom',
        3 => 'country 3',
    ),
    'category 2' => array(
        7 => 'country 7',
        2 => 'United Kingdom',
        8 => 'country 8,
    ),
);
echo CHtml::dorpDownList('country', $country_id, $countries);


The dropDownList will show something like:

category 1
country 1
United Kingdom
country 2

category 2
country 7
United Kingdom
country 8


:)
0

#3 User is offline   Asgaroth 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 371
  • Joined: 17-July 09
  • Location:Bogota, Colombia

Posted 06 May 2011 - 10:55 AM

And why would you want a duplicated entry in your list anyways?
- Extension: Dynamic DataProvider Behavior
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.



- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
0

#4 User is offline   tri 

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

Posted 06 May 2011 - 11:40 AM

View Postandyofb, on 06 May 2011 - 06:13 AM, said:

I want to have a dropdown list with

United Kingdom

Country 1
Country 2
Country 3
Country 4
Country 5
Country 6
Country 7
Country 8
United Kingdom
Country 9
Country 10
Country 11

How do I achieve this as United kingdom has the same value in the option (therefore I cant pass an array with the same key twice)

Thanks


In CHtml::listOptions() or CHtml::dropDownList (I tried the latter), the htmlOption empty can do it.
  array(
    'empty'=>array('', 'someindex'=>'somelabel', 'otherindex'=>'otherlabel'),
  )

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
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