Class yii\bootstrap\ButtonGroup

Inheritanceyii\bootstrap\ButtonGroup » yii\bootstrap\Widget » yii\base\Widget
Uses Traitsyii\bootstrap\BootstrapWidgetTrait
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-bootstrap/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\bootstrap\ButtonGroup
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap\BootstrapWidgetTrait
$clientOptions array The options for the underlying Bootstrap JS plugin. yii\bootstrap\BootstrapWidgetTrait
$encodeLabels boolean Whether to HTML-encode the button labels. yii\bootstrap\ButtonGroup
$options array The HTML attributes for the widget container tag. yii\bootstrap\Widget

Public Methods

Hide inherited methods

Method Description Defined By
getView() yii\bootstrap\BootstrapWidgetTrait
init() Initializes the widget. yii\bootstrap\ButtonGroup
run() Renders the widget. yii\bootstrap\ButtonGroup

Protected Methods

Hide inherited methods

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

getView() public abstract method

Defined in: yii\bootstrap\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.

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-group']);
}

            
registerClientEvents() protected method (available since version 2.0.2)

Defined in: yii\bootstrap\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\bootstrap\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();
}

            
renderButtons() protected method

Generates the buttons that compound the group as specified on $buttons.

protected string renderButtons ( )
return string

The rendering result.

                protected function renderButtons()
{
    $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;
            }
            $buttons[] = Button::widget($button);
        } else {
            $buttons[] = $button;
        }
    }
    return implode("\n", $buttons);
}

            
run() public method

Renders the widget.

public void run ( )

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