Harlan Gray
Harlan Gray

Reputation: 341

Classes on optgroup in yii 1

Is it possible to create a drop-down in yii 1 where optgroups have classes? I want to use $form->dropDownList()

eg:

<select>
  <optgroup label="Swedish Cars" class="swedish">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars" class="german">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select>

Upvotes: 1

Views: 165

Answers (1)

BlueZed
BlueZed

Reputation: 859

I just looked at the documentation (http://www.yiiframework.com/doc/api/1.1/CHtml#activeDropDownList-detail) and source code and there is nothing implemented to set the class, or any other attribute for the <optgroup> tag (see here: https://github.com/yiisoft/yii/blob/1.1.19/framework/web/helpers/CHtml.php#L2547).

The only way I can see for you to achieve this is to implement your own class, extending CHtml, and override the function listOptions() with your own implementation. That could then handle any additional options for optgroup classes as you wish.

Note that with this approach you then can't use $form->dropDownList() but would have to use YourNewClass::activeDropDownList() instead.

Hope this helps...

Upvotes: 1

Related Questions