yii2 checkboxList 自定义类 [英] yii2 checkboxList custom class

查看:40
本文介绍了yii2 checkboxList 自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 Yii2 checkboxList 的示例代码,我想为 checkboxList 中的每个项目添加自定义类,但我不知道如何以及在哪里添加它!
你能帮我吗..

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

提前致谢.

推荐答案

如果你想添加相同的类,你应该使用 itemOptions :

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',
    ]);
}]);

阅读更多:http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#checkboxList()-detail

添加示例

这篇关于yii2 checkboxList 自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆