Class yii\bootstrap5\ButtonGroup
| Inheritance | yii\ |
|---|---|
| Uses Traits | yii\ |
| Source Code | https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/ButtonGroup.php |
ButtonGroup renders a button group bootstrap component.
For example,
// a button group with items configuration
echo ButtonGroup::widget([
'buttons' => [
['label' => 'A'],
['label' => 'B'],
['label' => 'C', 'visible' => false],
]
]);
// button group with an item as a string
echo ButtonGroup::widget([
'buttons' => [
Button::widget(['label' => 'A']),
['label' => 'B'],
]
]);
Pressing on the button should be handled via JavaScript. See the following for details:
See also:
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $buttons | array | List of buttons. | yii\ |
| $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 to HTML-encode the button labels. | yii\ |
| $options | array | The HTML attributes for the widget container tag. | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| init() | 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\ |
| renderButtons() | Generates the buttons that compound the group as specified on $buttons. | yii\ |
Property Details
Whether to HTML-encode the button labels.
Method Details
| public void init ( ) |
public function init(): void
{
parent::init();
Html::addCssClass($this->options, [
'widget' => 'btn-group',
]);
if (!isset($this->options['role'])) {
$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);
}
}
Generates the buttons that compound the group as specified on $buttons.
| protected string renderButtons ( ) | ||
| return | string |
The rendering result. |
|---|---|---|
| throws | Throwable | |
protected function renderButtons(): string
{
$buttons = [];
foreach ($this->buttons as $button) {
if (is_array($button)) {
$visible = ArrayHelper::remove($button, 'visible', true);
if ($visible === false) {
continue;
}
$button['view'] = $this->getView();
if (!isset($button['encodeLabel'])) {
$button['encodeLabel'] = $this->encodeLabels;
}
if (!isset($button['options'], $button['options']['type'])) {
ArrayHelper::setValue($button, 'options.type', 'button');
}
$buttons[] = Button::widget($button);
} else {
$buttons[] = $button;
}
}
return implode("\n" , $buttons);
}