简体   繁体   English

Cakephp为多个复选框子组设置默认值

[英]Cakephp set default value for multiple checkbox subgroups

How to set default selected checkboxes when using "sub-groups" of checkboxes? 使用复选框的“子组”时如何设置默认选中的复选框? You can achieve the so-called "sub-groups" by having a nested array for 'options' attribute. 您可以通过为'options'属性设置嵌套数组来实现所谓的“子组”。 I want to have all checkboxes selected by default. 我想默认选中所有复选框。

echo $this->Form->input('fooBar', [
    'type' => 'select',
    'multiple' => 'checkbox',
    'label' => false,
    'class' => 'inlineBlock',
    'options' => [
        'Foo' => [0 => 'Yes', 1 => 'No' ]
        'Bar' => [0 => 'One', 1 => 'Two', 2 => 'Three']
]]);

I've tried by setting 我试过设置

'selected' => [0 => [0, 1], 0 => [0, 1, 2]] 'selected'=> [0 => [0,1],0 => [0,1,2]]

and

'checked' => true 'checked'=> true

but no luck. 但没有运气。 If I check them manually and than submit the form, Cakephp generates URL parameters as if there were no "sub-groups", eg: 如果我手动检查它们而不是提交表单,Cakephp会生成URL参数,就像没有“子组”一样,例如:

'options' => [0 => 'Yes', 1 => 'No' ] 'options'=> [0 =>'是',1 =>'否']

which is the same behaviour as if I set 这与我设置的行为相同

'default' => [1, 2, 3] 'default'=> [1,2,3]

Is it even possible to set selected checkboxes of individual checkbox "sub-groups"? 是否可以设置单个复选框“子组”的选定复选框?

You need to use the 'val' attributes like below : 您需要使用如下所示的'val'属性:

                    <?= $this->Form->control("themes._ids", [
                        "label" => false,
                        'multiple' => 'checkbox',
                        "val" => [1, 2, 3]
                    ]) ?>

Reference API Doc 参考API文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM