5

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?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

The button group won't work with the auto generated checkboxes because yii2 adds divs and the help block for errors. So what you can do is create a hidden form and connect the button group to it through jQuery. I have created the code you would need and got it working in my yii setup. All you need to do is replace <model name> with the name of your model.

<?php
use yii\bootstrap\ButtonGroup;
use yii\bootstrap\ActiveForm;
use yii\web\View;
?>

<?=
    ButtonGroup::widget([
        'buttons' => [
            ['label' => 'Checkbox 1', 'options'=>['class'=>'btn btn-primary', 'id'=>'button1', 'autocomplete'=>'off', 'aria-pressed'=>'false']],
            ['label' => 'Checkbox 2', 'options'=>['class'=>'btn btn-primary', 'id'=>'button2', 'autocomplete'=>'off', 'aria-pressed'=>'false']],
            ['label' => 'Checkbox 3', 'options'=>['class'=>'btn btn-primary', 'id'=>'button3', 'autocomplete'=>'off', 'aria-pressed'=>'false']],
        ]
    ]);
?>

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'field1')->hiddenInput()->label(false) ?>
<?= $form->field($model, 'field2')->hiddenInput()->label(false) ?>
<?= $form->field($model, 'field3')->hiddenInput()->label(false) ?>

<?php ActiveForm::end();?>

<?php
$script = <<< JS

if($('#<model name>-field1').val()=='1'){
    $('#button1').addClass('active');
    $('#button1').attr('aria-pressed', 'true');
}

if($('#<model name>-field2').val()=='1'){
    $('#button2').addClass('active');
    $('#button2').attr('aria-pressed', 'true');
}

if($('#<model name>-field3').val()=='1'){
    $('#button3').addClass('active');
    $('#button3').attr('aria-pressed', 'true');
}

$('.btn').on('click', function () {
    $(this).button('toggle')
    $(this).blur();
});

$('#button1').on('click', function () {
    if($('#button1').attr('aria-pressed')== 'true'){
        $('#<model name>-field1').val('1')
    } else {
        $('#<model name>-field1').val('0')
    }
});

$('#button2').on('click', function () {
    if($('#button2').attr('aria-pressed')== 'true'){
        $('#<model name>-field2').val('1')
    } else {
        $('#<model name>-field2').val('0')
    }
});

$('#button3').on('click', function () {
    if($('#button3').attr('aria-pressed')== 'true'){
        $('#<model name>-field3').val('1')
    } else {
        $('#<model name>-field3').val('0')
    }
});

JS;
$this->registerJs($script, View::POS_END);
?>

Comments

0

Please take a look to Class yii\bootstrap\ToggleButtonGroup

You can do somethink like

<?= $form->field($model, 'item_id')->widget(\yii\bootstrap\ToggleButtonGroup::classname(), [
    // configure additional widget properties here
]) ?>

with or without $form and $model

Available since version 2.0.6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.