【问题标题】:yii2 checkboxList custom classyii2 checkboxList 自定义类
【发布时间】:2015-02-13 07:31:59
【问题描述】:

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

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

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

提前致谢。

【问题讨论】:

    标签: yii2


    【解决方案1】:

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

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

    或者如果你想为每个项目自定义一个类,你应该使用项目回调:

    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 API 文档中看到了这个。我只想为每个项目添加不同的类。我必须为每个项目编写自定义函数?让我知道如何为每个项目添加类。非常感谢您的精彩重播。
    • 你只需要一个函数,看看我的例子和文档。而且我猜不出“我只想为每个Item添加不同的类”背后的原因。
    • 非常好!我使用 $index 变量来创建自定义类名!太感谢了 ! " 'class' => 'woshiClass'.$index,"
    【解决方案2】:

    以防万一您只需要更改标签选项:

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

    注意:您在itemOptions 中放置的所有内容都将在创建每个复选框时作为其自己的选项传递给Html::checkbox()。这意味着你可以通过classstylelabellabelOptions等。

    【讨论】:

    • 这适用于为复选框标签应用样式。谢谢。
    猜你喜欢
    • 2018-09-12
    • 2015-03-06
    • 2015-06-28
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多