css - Cakephp 3 - radio 输入标签的自定义类

标签 css templates cakephp radio-button cakephp-3.x

我正在创建一个多 radio 传递选项:

$this->Form->radio(
    'edu_tests_items.0.edu_sheets_items_grade_id',
     $eduSheetsGradesItems,
     ['autocomplete' => 'off']
);

每个 $eduSheetsGradesItems 的内容如下:

(int) 0 => [
    'value' => (int) 1,
    'text' => 'Mal',
    'label' => [
        'class' => 'btn btn-danger'
    ],
    'class' => 'btn btn-danger'
],

然后我为每个单选按钮组都得到了这个 html(这里我只显示第一个标签元素)

<input name="edu_tests_items[0][edu_sheets_items_grade_id]" value="" type="hidden">
<label for="edu-tests-items-0-edu-sheets-items-grade-id-1">
    <input
        name="edu_tests_items[0][edu_sheets_items_grade_id]"
        value="1"
        class="btn btn-danger"
        id="edu-tests-items-0-edu-sheets-items-grade-id-1"
        autocomplete="off"
        type="radio">
    Mal
</label>

我想做的是为标签元素(示例中的 btn btn-danger )设置一个特定的类(每个单选按钮标签都不同),但我不知道如何.

最佳答案

radio widget 不支持单独的radio label 配置,它只支持配置所有labels ( current not properly documented in the Cookbook ),可以通过radio() 方法的第三个参数传递,比如:

$this->Form->radio(
    'edu_tests_items.0.edu_sheets_items_grade_id',
    $eduSheetsGradesItems,
    [
        'autocomplete' => 'off',
        'label' => [
            'class' => 'btn btn-danger'
        ]
    ]
);

你也可以通过模板来配置它,但是它只能使用 FormHelper::control/input() 方法工作,但你实际上不必这样做。

所以如果你想对所有标签应用相同的类,那么上面的方法就可以了。如果您需要应用不同的类做不同的标签,那么您必须使用自定义小部件,例如扩展 Cake\View\Widget\RadioWidget 并覆盖 RadioWidget: :_renderLabel() 方法支持 $radio 参数中可能的 label 键,它映射到您的个人选项集。

这是一个简单粗暴的例子:

protected function _renderLabel($radio, $label, $input, $context, $escape)
{
    if ($label &&
        isset($radio['label'])
    ) {
        $label = is_array($label) ? $label : [];

        if (isset($label['class'])) {
            $radio['label'] = $this->_templates->addClass($radio['label'], $label['class']);
        }

        $label = $radio['label'] + $label;
    }

    return parent::_renderLabel($radio, $label, $input, $context, $escape);
}

另见

关于css - Cakephp 3 - radio 输入标签的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240899/

相关文章:

templates - 使用 Django 模板标签的 Jinja2 模板

templates - 是否可以在 Julia 中声明对任何结构的一般引用?

javascript - jQuery 选择器优化

python - 在 jinja 中设置变量

mysql - CakePHP 3 : Database syntax error or access violation issue

php - CakePHP中的Cron Jobs突然停止了

php - CakePHP 3.x 基于用户组/组织/团队过滤查询结果

javascript - Owl Carousel 更改导航编号

css - 不同浏览器的单一CSS

javascript - Vue2 : how to get a smooth mouse move?