类 在 yii 1 中的 optgroup

Classes on optgroup in yii 1

是否可以在 yii 1 中创建一个下拉列表,其中 optgroups 有 类? 我想使用 $form->dropDownList()

例如:

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

我刚刚看了文档 (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)。

我能看到您实现此目的的唯一方法是实现您自己的 class,扩展 CHtml,并用您自己的实现覆盖函数 listOptions()。然后可以根据需要处理 optgroup classes 的任何其他选项。

请注意,使用此方法后,您将无法使用 $form->dropDownList(),而必须使用 YourNewClass::activeDropDownList()

希望这对您有所帮助...