3

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

1
  • P.S To use a placeholder use prompt instead of empty Commented Mar 10, 2013 at 21:08

4 Answers 4

3

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'),
   ); 
?>
Sign up to request clarification or add additional context in comments.

3 Comments

i have tried that but it does not work; i used chrome and inspected the dropdown and no class...what do you suggest..
you get a class in the select tag. do you want a class on each option tag?
ok it works. It was working because i had sth. extra added that i left out in my question...ive made an edit in my question so u can see it..@naselorm samething, your suggestion works too thanks guys..
1

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'),

Comments

0

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

Comments

0

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...')
); 
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.