konapaz
(Konapaz)
1
Hi
Is there way to style a specific generated option using CHtml::dropDownList ?
I want to achieve something like that
<select name="a_name" id="an_id">
<option value="1" class="marked">label 1</option>
<option value="2">label 2</option>
<option value="3" class="marked">label 3</option>
<option value="4">label 4</option>
</select>
Thanks
Dave
(Deventer)
2
According to the code you can set it within the htmlOptions array().
The structure should be like:
$htmlOptions = array(
'options' => array(
'<keyOfTheEntryMostlyThePk>' => array(
'class' => 'your desired class'
');
)
);
You can find the entire code in CHtml::listOptions(); Here is an excerpt, take a look at the last few lines starting with "if(isset($options[$key]))"
foreach($listData as $key=>$value)
{
if(is_array($value))
{
$content.='<optgroup label="'.($raw?$key : self::encode($key))."\">\n";
$dummy=array('options'=>$options);
if(isset($htmlOptions['encode']))
$dummy['encode']=$htmlOptions['encode'];
$content.=self::listOptions($selection,$value,$dummy);
$content.='</optgroup>'."\n";
}
else
{
$attributes=array('value'=>(string)$key,'encode'=>!$raw);
if(!is_array($selection) && !strcmp($key,$selection) || is_array($selection) && in_array($key,$selection))
$attributes['selected']='selected';
if(isset($options[$key]))
$attributes=array_merge($attributes,$options[$key]);
$content.=self::tag('option',$attributes,$raw?(string)$value : self::encode((string)$value))."\n";
}
}
I did not tested the code, I just took a quick look at the code, so sorry if I missed something 
konapaz
(Konapaz)
3
Thanks!
If this is the only Yii way then another way is using pure php to generate html dropdownlist or extends the Chtml::dropdownlist to do this 