Class yii\bootstrap5\ToggleButtonGroup
| Inheritance | yii\ |
|---|---|
| Uses Traits | yii\ |
| Source Code | https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/ToggleButtonGroup.php |
ToggleButtonGroup allows rendering form inputs Checkbox/Radio toggle button groups.
You can use this widget in an ActiveForm using the yii\
<?= $form->field($model, 'item_id')->widget(\yii\bootstrap5\ToggleButtonGroup::class, [
'type' => \yii\bootstrap5\ToggleButtonGroup::TYPE_CHECKBOX
'items' => [
'fooValue' => 'BarLabel',
'barValue' => 'BazLabel'
]
]) ?>
See also https://getbootstrap.com/docs/5.1/components/buttons/#checkbox-and-radio-buttons.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $clientEvents | array | The event handlers for the underlying Bootstrap JS plugin. | yii\ |
| $clientOptions | array|false | The options for the underlying Bootstrap JS plugin/component. | yii\ |
| $encodeLabels | boolean | Whether the items labels should be HTML-encoded. | yii\ |
| $items | array | The data item used to generate the checkboxes. | yii\ |
| $labelOptions | yii\ |
||
| $type | string | Input type, can be TYPE_CHECKBOX or TYPE_RADIO | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| init() | yii\ |
|
| renderItem() | Default callback for checkbox/radio list item rendering. | yii\ |
| run() | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| registerClientEvents() | Registers JS event handlers that are listed in $clientEvents. | yii\ |
| registerPlugin() | Registers a specific Bootstrap plugin/component and the related events. | yii\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| TYPE_CHECKBOX | 'checkbox' | Checkbox type | yii\ |
| TYPE_RADIO | 'radio' | Radio type | yii\ |
Property Details
Whether the items labels should be HTML-encoded.
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.
See also:
- \
yii\ bootstrap5\ Html::checkbox() - \
yii\ bootstrap5\ Html::radio()
Method Details
| public void init ( ) |
public function init(): void
{
parent::init();
$this->registerPlugin('button');
Html::addCssClass($this->options, [
'widget' => 'btn-group',
]);
$this->options['role'] = 'group';
}
Defined in:
yii\
Registers JS event handlers that are listed in $clientEvents.
| protected void registerClientEvents ( ?string $name = null ) | ||
| $name | ?string | |
protected function registerClientEvents(?string $name = null): void
{
if (!empty($this->clientEvents)) {
$id = $this->options['id'];
$js = [];
$appendix = ($name === 'dropdown') ? '.parentElement' : '';
foreach ($this->clientEvents as $event => $handler) {
$js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);";
}
$this->getView()->registerJs(implode("\n" , $js));
}
}
Defined in:
yii\
Registers a specific Bootstrap plugin/component and the related events.
| protected void registerPlugin ( string $name ) | ||
| $name | string |
The name of the Bootstrap plugin |
protected function registerPlugin(string $name): void
{
/**
* @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
*/
$jsPlugins = [
'alert',
'button',
'carousel',
'collapse',
'dropdown',
'modal',
'offcanvas',
'popover',
'scrollspy',
'tab',
'toast',
'tooltip',
];
if (in_array($name, $jsPlugins, true)) {
$view = $this->getView();
BootstrapPluginAsset::register($view);
// 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
if ($this->clientOptions !== false || in_array($name, ['popover', 'toast', 'tooltip'], true)) {
$name = ucfirst($name);
$id = $this->options['id'];
$options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
$view->registerJs("(new bootstrap.$name('#$id', $options));");
}
$this->registerClientEvents($name);
}
}
Default callback for checkbox/radio list item rendering.
See also:
- \
yii\ bootstrap5\ Html::checkbox() - \
yii\ bootstrap5\ Html::radio()
| public string renderItem ( integer $index, string $label, string $name, boolean $checked, string $value ) | ||
| $index | integer |
Item index. |
| $label | string |
Item label. |
| $name | string |
Input name. |
| $checked | boolean |
Whether value is checked or not. |
| $value | string |
Input value. |
| return | string |
Generated HTML. |
|---|---|---|
public function renderItem(int $index, string $label, string $name, bool $checked, string $value): string
{
unset($index);
$labelOptions = $this->labelOptions;
$labelOptions['wrapInput'] = false;
Html::addCssClass($labelOptions, [
'widget' => 'btn',
]);
$type = $this->type;
if ($this->encodeLabels) {
$label = Html::encode($label);
}
$options = [
'label' => $label,
'labelOptions' => $labelOptions,
'autocomplete' => 'off',
'value' => $value,
];
Html::addCssClass($options, [
'widget' => 'btn-check',
]);
return Html::$type($name, $checked, $options);
}
| public string run ( ) | ||
| throws | \ |
|
|---|---|---|
public function run(): string
{
if (!isset($this->options['item'])) {
$this->options['item'] = [$this, 'renderItem'];
}
switch ($this->type) {
case 'checkbox':
if ($this->hasModel()) {
return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
} else {
return Html::checkboxList($this->name, $this->value, $this->items, $this->options);
}
// no break
case 'radio':
if ($this->hasModel()) {
return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
} else {
return Html::radioList($this->name, $this->value, $this->items, $this->options);
}
// no break
default:
throw new InvalidConfigException("Unsupported type '{$this->type}'");
}
}