Blame view

vendor/yiisoft/yii2-bootstrap/ToggleButtonGroup.php 3.14 KB
2e86c939   xu   “首次提交”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\bootstrap;

use yii\base\InvalidConfigException;

/**
 * ToggleButtonGroup allows rendering form inputs Checkbox/Radio toggle button groups.
 *
 * You can use this widget in an [[yii\bootstrap\ActiveForm|ActiveForm]] using the [[yii\widgets\ActiveField::widget()|widget()]]
 * method, for example like this:
 *
 * ```php
 * <?= $form->field($model, 'item_id')->widget(\yii\bootstrap\ToggleButtonGroup::classname(), [
 *     // configure additional widget properties here
 * ]) ?>
 * ```
 *
 * @see http://getbootstrap.com/javascript/#buttons-checkbox-radio
 *
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0.6
 */
class ToggleButtonGroup extends InputWidget
{
    /**
     * @var string input type, can be:
     * - 'checkbox'
     * - 'radio'
     */
    public $type;
    /**
     * @var array the data item used to generate the checkboxes.
     * The array values are the labels, while the array keys are the corresponding checkbox or radio values.
     */
    public $items = [];
    /**
     * @var array, the HTML attributes for the label (button) tag.
     * @see Html::checkbox()
     * @see Html::radio()
     */
    public $labelOptions = [];
    /**
     * @var boolean whether the items labels should be HTML-encoded.
     */
    public $encodeLabels = true;


    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        $this->registerPlugin('button');
        Html::addCssClass($this->options, 'btn-group');
        $this->options['data-toggle'] = 'buttons';
    }

    /**
     * @inheritdoc
     */
    public function run()
    {
        if (!isset($this->options['item'])) {
            $this->options['item'] = [$this, 'renderItem'];
        }
        switch ($this->type) {
            case 'checkbox':
                return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
            case 'radio':
                return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
            default:
                throw new InvalidConfigException("Unsupported type '{$this->type}'");
        }
    }

    /**
     * Default callback for checkbox/radio list item rendering.
     * @param integer $index item index.
     * @param string $label item label.
     * @param string $name input name.
     * @param boolean $checked whether value is checked or not.
     * @param string $value input value.
     * @return string generated HTML.
     * @see Html::checkbox()
     * @see Html::radio()
     */
    public function renderItem($index, $label, $name, $checked, $value)
    {
        $labelOptions = $this->labelOptions;
        Html::addCssClass($labelOptions, 'btn');
        if ($checked) {
            Html::addCssClass($labelOptions, 'active');
        }
        $type = $this->type;
        if ($this->encodeLabels) {
            $label = Html::encode($label);
        }
        return Html::$type($name, $checked, ['label' => $label, 'labelOptions' => $labelOptions, 'value' => $value]);
    }
}