Class yii\bootstrap5\ButtonToolbar

Inheritanceyii\bootstrap5\ButtonToolbar » yii\bootstrap5\Widget » yii\base\Widget
Uses Traitsyii\bootstrap5\BootstrapWidgetTrait
Source Code https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/ButtonToolbar.php

ButtonToolbar Combines sets of button groups into button toolbars for more complex components.

Use utility classes as needed to space out groups, buttons, and more.

For example,

// a button toolbar with items configuration
echo ButtonToolbar::widget([
    'buttonGroups' => [
        [
            'buttons' => [
                ['label' => '1', 'options' => ['class' => ['btn-secondary']]],
                ['label' => '2', 'options' => ['class' => ['btn-secondary']]],
                ['label' => '3', 'options' => ['class' => ['btn-secondary']]],
                ['label' => '4', 'options' => ['class' => ['btn-secondary']]]
            ],
             'class' => ['mr-2']
        ],
        [
            'buttons' => [
                ['label' => '5', 'options' => ['class' => ['btn-secondary']]],
                ['label' => '6', 'options' => ['class' => ['btn-secondary']]],
                ['label' => '7', 'options' => ['class' => ['btn-secondary']]]
            ],
            'class' => ['mr-2']
        ],
        [
            'buttons' => [
                ['label' => '8', 'options' => ['class' => ['btn-secondary']]]
            ]
        ]
    ]
]);

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
$buttonGroups array List of buttons groups. yii\bootstrap5\ButtonToolbar
$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
$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
renderButtonGroups() Generates the button groups that compound the toolbar as specified on $buttonGroups. yii\bootstrap5\ButtonToolbar

Property Details

Hide inherited properties

$buttonGroups public property

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

  • buttons: array list of buttons. Either as array or string representation
  • options: array optional, the HTML attributes of the button group.
  • encodeLabels: bool whether to HTML-encode the button labels.
public array $buttonGroups = []

Method Details

Hide inherited methods

init() public method

public void init ( )

                public function init(): void
{
    parent::init();
    Html::addCssClass($this->options, [
        'widget' => 'btn-toolbar',
    ]);
    if (!isset($this->options['role'])) {
        $this->options['role'] = 'toolbar';
    }
}

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

            
renderButtonGroups() protected method

Generates the button groups that compound the toolbar as specified on $buttonGroups.

protected string renderButtonGroups ( )
return string

The rendering result.

throws Throwable

                protected function renderButtonGroups(): string
{
    $buttonGroups = [];
    foreach ($this->buttonGroups as $group) {
        if (is_array($group)) {
            $group['view'] = $this->getView();
            if (!isset($group['buttons'])) {
                continue;
            }
            $buttonGroups[] = ButtonGroup::widget($group);
        } else {
            $buttonGroups[] = $group;
        }
    }
    return implode("\n", $buttonGroups);
}

            
run() public method

public string run ( )
throws Throwable

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