Reputation: 353
I am using Yii 1.1.10. I would like to know how to add a CSS class to a dropdown list. I am using a CActiveForm
for example, how would i add a CSS class to this dropdown list?
<?php echo $form->labelEx($model,'chassis'); ?>
<?php echo $form->dropDownList($model, 'chassis',
array('saloon' => 'saloon', 'station wagon' => 'station wagon', ),
);
?>
EDIT: I had this in my code
array('empty' => 'Select one of the following...')
i had it there to make it the default message. but somehow it interfered with using
'htmlOptions'=>array('class'=>'yourCssClass')
OR
array('class'=>'your_class_name')
so as long as i remove it, both suggestions work!Thank guys
Upvotes: 3
Views: 17127
Reputation: 737
This is an answer. :)
<?php echo $form->labelEx($model,'chassis'); ?>
<?php echo $form->dropDownList($model, 'chassis',
array('saloon' => 'saloon', 'station wagon' => 'station wagon', ),
array('class'=>'form-control','empty' => 'Select one of the following...')
);
?>
Upvotes: 0
Reputation: 1210
Array values are displayed and array keys are stored.
This is Model code.
public function getCityOptions()
{
$list=array('saloon' => 'saloon', 'station wagon' => 'station wagon',);
asort($list);
return $list;
}
This the _form.php code
<div class="row">
<?php echo $form->labelEx($model,'chassis'); ?>
<?php echo $form->dropDownList($model,'chassis',$model->getCityOptions()); ?>
<?php echo $form->error($model,'chassis'); ?>
</div>
try this..
Upvotes: 0
Reputation: 33
This is the syntax for dropDownList according to Yiiframework API
public string dropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))
The last part enables you to pass any html values as you would normally do in an html page.Thus add this after station wagon,
'htmlOptions'=>array('class'=>'yourCssClass'),
Upvotes: 1
Reputation: 4114
You can use htmlOptions
argument of CActiveFrom::dropDownList()
to specify class
,style
or any other HTML attributes.
<?php echo $form->labelEx($model,'chassis'); ?>
<?php echo $form->dropDownList($model, 'chassis',
array('saloon' => 'saloon', 'station wagon' => 'station wagon', ),
array('class'=>'your_class_name'),
);
?>
Upvotes: 3