Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 checkbox group with bootstrap 3

How to create checkbox group in yii2?

enter image description here

That's what we need

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary active">
    <input type="checkbox" autocomplete="off" checked> Checkbox 1 (pre-checked)
  </label>
  <label class="btn btn-primary">
    <input type="checkbox" autocomplete="off"> Checkbox 2
  </label>
  <label class="btn btn-primary">
    <input type="checkbox" autocomplete="off"> Checkbox 3
  </label>
</div>


That's what I have

<?
    $options = ['uncheck'=>0];

    echo ButtonGroup::widget([
        'options' => [
            'data-toggle' => 'buttons'
        ],
        'buttons' => [
            $form->field($model, 'field1')->checkbox($options),
            $form->field($model, 'field2')->checkbox($options),
            $form->field($model, 'field3')->checkbox($options),
        ],
    ]);
?>


What I need to add in my code, to generate that markdown?

like image 487
Alex Avatar asked Nov 09 '22 07:11

Alex


1 Answers

My variant. I used standard yii radiobox and customize template.

<?= $form->field($model, 'attribute')->radioList(
[
        1 => 'Enabled',
        2 => 'Disabled'
    ],
    [
        'item' => function ($index, $label, $name, $checked, $value) {
            if ($value==1)
                $class_btn =  'btn-success'; // Style for enable
            else
                $class_btn = 'btn-default'; // Style for disable

            if ($checked)
                $class_btn = $class_btn.' active'; // Style for checked button
            return
                '<label class="btn '. $class_btn.'">' . Html::radio($name, $checked, ['value' => $value]) . $label . '</label>';
        },
        'class' => 'btn-group', "data-toggle"=>"buttons", // Bootstrap class for Button Group
    ]
)->label('Some label');
?>

My result

like image 156
Nikolay Shayahmetov Avatar answered Nov 15 '22 12:11

Nikolay Shayahmetov