Class yii\bootstrap5\ButtonGroup

Inheritanceyii\bootstrap5\ButtonGroup » yii\bootstrap5\Widget » yii\base\Widget
Uses Traitsyii\bootstrap5\BootstrapWidgetTrait
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

Hide inherited properties

Property Type Description Defined By
$buttons array List of buttons. yii\bootstrap5\ButtonGroup
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap5\BootstrapWidgetTrait
$clientOptions array|false The options for the underlying Bootstrap JS plugin/component. yii\bootstrap5\BootstrapWidgetTrait
$encodeLabels boolean Whether to HTML-encode the button labels. yii\bootstrap5\ButtonGroup
$options array The HTML attributes for the widget container tag. yii\bootstrap5\Widget

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap5\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin/component and the related events. yii\bootstrap5\BootstrapWidgetTrait
renderButtons() Generates the buttons that compound the group as specified on $buttons. yii\bootstrap5\ButtonGroup

Property Details

Hide inherited properties

$buttons public property

List of buttons. Each array element represents a single button which can be specified as a string or an array of the following structure:

  • label: string, required, the button label.
  • options: array, optional, the HTML attributes of the button.
  • visible: bool, optional, whether this button is visible. Defaults to true.
public array $buttons = []
$encodeLabels public property

Whether to HTML-encode the button labels.

public boolean $encodeLabels true

Method Details

Hide inherited methods

init() public method

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';
    }
}

            
registerClientEvents() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerClientEvents()

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));
    }
}

            
registerPlugin() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerPlugin()

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);
    }
}

            
renderButtons() protected method

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);
}

            
run() public method

public string run ( )
throws Throwable

                public function run(): string
{
    BootstrapAsset::register($this->getView());
    return Html::tag('div', $this->renderButtons(), $this->options);
}