2

Working with Yii and active checkboxlist. I know the params. I need to add a flag css class to the items. This is my code:

$form->checkBoxList($model, 'items', $selected, array(
    'class'=>'default_class'
));

This code just adds a default_class to every item. But I need a different class for specific items.

4
  • You can use a CSS pseudo class: .mycheckboxes:checked {...} Commented Feb 2, 2013 at 11:02
  • You're right. I made a wrong question though! Commented Feb 2, 2013 at 11:04
  • I am also facing prob like this. Can you tell in this case $selected is what....? Commented Sep 22, 2014 at 14:46
  • @NaincyGupta the $selected is the item(s) that you want to be selected. Check the Yii documents. Commented Sep 24, 2014 at 4:17

2 Answers 2

1

Asked the same question in the Yii forum. Someone helped me with this solution

foreach ($models as $model) {
  echo '<input type="checkbox" name="' . CHtml::activeName($model, 'attribute') . '[]" value="' . $model->valueField . '" ' . condition ? 'class= "your-class" : '' . '/>';
}

This is a good lead for now.

Sign up to request clarification or add additional context in comments.

Comments

0

@XIII, I had updated my answer

$form->checkBoxList($model, 'items', $selected, array(
  'options' => array(
       'value1'=>array('disabled'=>true, 'label'=>'value 1'),
       'value2'=>array('label'=>'value 2', 'class' => 'css-class-defined'),
   ),
));

Please read docs about function what you used, or see source code system.web.helpers.CHtml line 764

4 Comments

I don't need the lableOption! This will add a class to all the labels, not the conditioned ones!
Why is this an accepted answer, there is nothing in the source to suggest this works.. and I just spent an hour trying to implement it.
@Arth, what version of Yii you use?
I'm using 1.1.15, but I referred to the online code.. you can see from the foreach on line 1050 that the options array simply isn't treated the way you suggest. It is interpreted as 'value'=>'labelTitle' and any attempt to use 'value'=>array(...) results in each check box being labelled 'Array' as the array is just printed straight out as a string.

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.