我正在创建一个多 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/