Class yii\bootstrap4\ButtonDropdown

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

ButtonDropdown renders a group or split button dropdown bootstrap component.

For example,

// a button group using Dropdown widget
echo ButtonDropdown::widget([
    'label' => 'Action',
    'dropdown' => [
        'items' => [
            ['label' => 'DropdownA', 'url' => '/'],
            ['label' => 'DropdownB', 'url' => '#'],
        ],
    ],
]);

See also:

Public Properties

Hide inherited properties

Property Type Description Defined By
$buttonOptions array The HTML attributes of the button. yii\bootstrap4\ButtonDropdown
$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
$direction string The drop-direction of the widget Possible values are 'left', 'right', 'up', or 'down' (default) yii\bootstrap4\ButtonDropdown
$dropdown array The configuration array for yii\bootstrap4\Dropdown. yii\bootstrap4\ButtonDropdown
$dropdownClass string Name of a class to use for rendering dropdowns withing this widget. yii\bootstrap4\ButtonDropdown
$encodeLabel boolean Whether the label should be HTML-encoded. yii\bootstrap4\ButtonDropdown
$label string The button label yii\bootstrap4\ButtonDropdown
$options array The HTML attributes for the container tag. yii\bootstrap4\ButtonDropdown
$renderContainer boolean Whether to render the container using the $options as HTML attributes. yii\bootstrap4\ButtonDropdown
$split boolean Whether to display a group of split-styled button group. yii\bootstrap4\ButtonDropdown
$tagName string The tag to use to render the button yii\bootstrap4\ButtonDropdown

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
renderButton() Generates the button dropdown. yii\bootstrap4\ButtonDropdown
renderDropdown() Generates the dropdown menu. yii\bootstrap4\ButtonDropdown

Constants

Hide inherited constants

Constant Value Description Defined By
DIRECTION_DOWN 'down' The css class part of dropdown yii\bootstrap4\ButtonDropdown
DIRECTION_LEFT 'left' The css class part of dropleft yii\bootstrap4\ButtonDropdown
DIRECTION_RIGHT 'right' The css class part of dropright yii\bootstrap4\ButtonDropdown
DIRECTION_UP 'up' The css class part of dropup yii\bootstrap4\ButtonDropdown

Property Details

Hide inherited properties

$buttonOptions public property

The HTML attributes of the button.

See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

public array $buttonOptions = []
$direction public property

The drop-direction of the widget

Possible values are 'left', 'right', 'up', or 'down' (default)

public string $direction self::DIRECTION_DOWN
$dropdown public property

The configuration array for yii\bootstrap4\Dropdown.

public array $dropdown = []
$dropdownClass public property

Name of a class to use for rendering dropdowns withing this widget. Defaults to yii\bootstrap4\Dropdown.

public string $dropdownClass 'yii\bootstrap4\Dropdown'
$encodeLabel public property

Whether the label should be HTML-encoded.

public boolean $encodeLabel true
$label public property

The button label

public string $label 'Button'
$options public property

The HTML attributes for the container tag. The following special options are recognized:

  • tag: string, defaults to "div", the name of the container tag.

See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

public array $options = []
$renderContainer public property

Whether to render the container using the $options as HTML attributes. If set to false, the container element enclosing the button and dropdown will NOT be rendered.

public boolean $renderContainer true
$split public property

Whether to display a group of split-styled button group.

public boolean $split false
$tagName public property

The tag to use to render the button

public string $tagName 'button'

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();
    if (!isset($this->buttonOptions['id'])) {
        $this->buttonOptions['id'] = $this->options['id'] . '-button';
    }
}

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

            
renderButton() protected method

Generates the button dropdown.

protected string renderButton ( )
return string

The rendering result.

throws Exception

                protected function renderButton()
{
    Html::addCssClass($this->buttonOptions, ['widget' => 'btn']);
    $label = $this->label;
    if ($this->encodeLabel) {
        $label = Html::encode($label);
    }
    if ($this->split) {
        $buttonOptions = $this->buttonOptions;
        $this->buttonOptions['data-toggle'] = 'dropdown';
        $this->buttonOptions['aria-haspopup'] = 'true';
        $this->buttonOptions['aria-expanded'] = 'false';
        Html::addCssClass($this->buttonOptions, ['toggle' => 'dropdown-toggle dropdown-toggle-split']);
        unset($buttonOptions['id']);
        $splitButton = Button::widget([
            'label' => '<span class="sr-only">Toggle Dropdown</span>',
            'encodeLabel' => false,
            'options' => $this->buttonOptions,
            'view' => $this->getView(),
        ]);
    } else {
        $buttonOptions = $this->buttonOptions;
        Html::addCssClass($buttonOptions, ['toggle' => 'dropdown-toggle']);
        $buttonOptions['data-toggle'] = 'dropdown';
        $buttonOptions['aria-haspopup'] = 'true';
        $buttonOptions['aria-expanded'] = 'false';
        $splitButton = '';
    }
    if (isset($buttonOptions['href'])) {
        if (is_array($buttonOptions['href'])) {
            $buttonOptions['href'] = Url::to($buttonOptions['href']);
        }
    } else {
        if ($this->tagName === 'a') {
            $buttonOptions['href'] = '#';
            $buttonOptions['role'] = 'button';
        }
    }
    return Button::widget([
            'tagName' => $this->tagName,
            'label' => $label,
            'options' => $buttonOptions,
            'encodeLabel' => false,
            'view' => $this->getView(),
        ]) . "\n" . $splitButton;
}

            
renderDropdown() protected method

Generates the dropdown menu.

protected string renderDropdown ( )
return string

The rendering result.

throws Exception

                protected function renderDropdown()
{
    $config = $this->dropdown;
    $config['clientOptions'] = false;
    $config['view'] = $this->getView();
    /** @var Widget $dropdownClass */
    $dropdownClass = $this->dropdownClass;
    return $dropdownClass::widget($config);
}

            
run() public method

public void run ( )
throws Exception

                public function run()
{
    $html = $this->renderButton() . "\n" . $this->renderDropdown();
    if ($this->renderContainer) {
        Html::addCssClass($this->options, ['widget' => 'drop' . $this->direction, 'btn-group']);
        $options = $this->options;
        $tag = ArrayHelper::remove($options, 'tag', 'div');
        $html = Html::tag($tag, $html, $options);
    }
    // Set options id to button options id to ensure correct css selector in plugin initialisation
    $this->options['id'] = $this->buttonOptions['id'];
    $this->registerPlugin('dropdown');
    return $html;
}