yii2 checkboxList custom class

你。 提交于 2019-12-24 05:34:13

问题


Here is the sample code from Yii2 checkboxList, I want to add custom class for each Item in checkboxList but I don't know how and where can I add that!
Could you please help me please ..

$list = [0 => 'PHP', 1 => 'MySQL', 2 => 'Javascript'];
$list2 = [0,2];

echo Html::checkboxList('CuisineId',$list2,$list,array('class' => 'test' ));

Thanks in advance.


回答1:


If you want to add the same class, you should use itemOptions :

echo Html::checkboxList('CuisineId', $list2, $list, ['itemOptions'=>['class' => 'test']]);

Or if you want a custom class for each item, you should use item callback :

echo Html::checkboxList('CuisineId', $list2, $list, ['item'=>function ($index, $label, $name, $checked, $value){
    return Html::checkbox($name, $checked, [
       'value' => $value,
       'label' => $label,
       'class' => 'any class',
    ]);
}]);

Read more : http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#checkboxList()-detail

EDIT : add example




回答2:


Just in case you only need to change the label options:

<?= Html::checkboxList('CuisineId', $list, $items, [
    'itemOptions' => [
        'labelOptions' => [
            'style' => 'font-weight: normal',
            'class' => 'some-custom-class',
        ],
    ],
]) ?>

Note: Everything you put inside itemOptions will be passed to Html::checkbox() as its own options when creating each checkbox. It means you can pass class, style, label, labelOptions, etc.



来源:https://stackoverflow.com/questions/27480061/yii2-checkboxlist-custom-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!