Class yii\bootstrap4\ButtonToolbar

Inheritanceyii\bootstrap4\ButtonToolbar » yii\bootstrap4\Widget » yii\base\Widget
Uses Traitsyii\bootstrap4\BootstrapWidgetTrait
Source Code https://github.com/yiisoft/yii2-bootstrap4/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\bootstrap4\ButtonToolbar
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap4\BootstrapWidgetTrait
$clientOptions array The options for the underlying Bootstrap JS plugin. yii\bootstrap4\BootstrapWidgetTrait
$options array The HTML attributes for the widget container tag. yii\bootstrap4\Widget

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap4\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin and the related events yii\bootstrap4\BootstrapWidgetTrait
renderButtonGroups() Generates the button groups that compound the toolbar as specified on $buttonGroups. yii\bootstrap4\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

getView() public abstract method

Defined in: yii\bootstrap4\BootstrapWidgetTrait::getView()

See also \yii\base\Widget::getView().

public abstract \yii\web\View getView ( )
return \yii\web\View

The view object that can be used to render views or view files.

                abstract function getView();

            
init() public method

Initializes the widget.

This method will register the bootstrap asset bundle. If you override this method, make sure you call the parent implementation first.

public void init ( )

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

            
registerClientEvents() protected method

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

Registers JS event handlers that are listed in $clientEvents.

protected void registerClientEvents ( )

                protected function registerClientEvents()
{
    if (!empty($this->clientEvents)) {
        $id = $this->options['id'];
        $js = [];
        foreach ($this->clientEvents as $event => $handler) {
            $js[] = "jQuery('#$id').on('$event', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerPlugin() protected method

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

Registers a specific Bootstrap plugin and the related events

protected void registerPlugin ( $name )
$name string

The name of the Bootstrap plugin

                protected function registerPlugin($name)
{
    $view = $this->getView();
    BootstrapPluginAsset::register($view);
    $id = $this->options['id'];
    if ($this->clientOptions !== false) {
        $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
        $js = "jQuery('#$id').$name($options);";
        $view->registerJs($js);
    }
    $this->registerClientEvents();
}

            
renderButtonGroups() protected method

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

protected string renderButtonGroups ( )
return string

The rendering result.

throws Exception

                protected function renderButtonGroups()
{
    $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 void run ( )
throws Exception

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