Class yii\bootstrap\Dropdown

Inheritanceyii\bootstrap\Dropdown » 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/Dropdown.php

Dropdown renders a Bootstrap dropdown menu component.

For example,

<div class="dropdown">
    <a href="#" data-toggle="dropdown" class="dropdown-toggle">Label <b class="caret"></b></a>
    <?php
        echo Dropdown::widget([
            'items' => [
                ['label' => 'DropdownA', 'url' => '/'],
                ['label' => 'DropdownB', 'url' => '#'],
            ],
        ]);
    ?>
</div>

See also http://getbootstrap.com/javascript/#dropdowns.

Public Properties

Hide inherited properties

Property Type Description Defined By
$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 the labels for header items should be HTML-encoded. yii\bootstrap\Dropdown
$items array List of menu items in the dropdown. yii\bootstrap\Dropdown
$options array The HTML attributes for the widget container tag. yii\bootstrap\Widget
$submenuOptions array|null The HTML attributes for sub-menu container tags. yii\bootstrap\Dropdown

Public Methods

Hide inherited methods

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

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
renderItems() Renders menu items. yii\bootstrap\Dropdown

Property Details

Hide inherited properties

$encodeLabels public property

Whether the labels for header items should be HTML-encoded.

public boolean $encodeLabels true
$items public property

List of menu items in the dropdown. Each array element can be either an HTML string, or an array representing a single menu with the following structure:

  • label: string, required, the label of the item link.
  • encode: bool, optional, whether to HTML-encode item label.
  • url: string|array, optional, the URL of the item link. This will be processed by \yii\helpers\Url::to(). If not set, the item will be treated as a menu header when the item has no sub-menu.
  • visible: bool, optional, whether this menu item is visible. Defaults to true.
  • linkOptions: array, optional, the HTML attributes of the item link.
  • options: array, optional, the HTML attributes of the item.
  • items: array, optional, the submenu items. The structure is the same as this property. Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it.
  • submenuOptions: array, optional, the HTML attributes for sub-menu container tag. If specified it will be merged with $submenuOptions.

To insert divider use <li role="presentation" class="divider"></li>.

public array $items = []
$submenuOptions public property (available since version 2.0.5)

The HTML attributes for sub-menu container tags. If not set - $options value will be used for it.

public array|null $submenuOptions null

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()
{
    if ($this->submenuOptions === null) {
        // copying of [[options]] kept for BC
        // @todo separate [[submenuOptions]] from [[options]] completely before 2.1 release
        $this->submenuOptions = $this->options;
        unset($this->submenuOptions['id']);
    }
    parent::init();
    Html::addCssClass($this->options, ['widget' => 'dropdown-menu']);
}

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

            
renderItems() protected method

Renders menu items.

protected string renderItems ( $items, $options = [] )
$items array

The menu items to be rendered

$options array

The container HTML attributes

return string

The rendering result.

throws \yii\base\InvalidConfigException

if the label option is not specified in one of the items.

                protected function renderItems($items, $options = [])
{
    $lines = [];
    foreach ($items as $item) {
        if (is_string($item)) {
            $lines[] = $item;
            continue;
        }
        if (isset($item['visible']) && !$item['visible']) {
            continue;
        }
        if (!array_key_exists('label', $item)) {
            throw new InvalidConfigException("The 'label' option is required.");
        }
        $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
        $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
        $itemOptions = ArrayHelper::getValue($item, 'options', []);
        $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
        $linkOptions['tabindex'] = '-1';
        $url = array_key_exists('url', $item) ? $item['url'] : null;
        if (empty($item['items'])) {
            if ($url === null) {
                $content = $label;
                Html::addCssClass($itemOptions, ['widget' => 'dropdown-header']);
            } else {
                $content = Html::a($label, $url, $linkOptions);
            }
        } else {
            $submenuOptions = $this->submenuOptions;
            if (isset($item['submenuOptions'])) {
                $submenuOptions = array_merge($submenuOptions, $item['submenuOptions']);
            }
            $content = Html::a($label, $url === null ? '#' : $url, $linkOptions)
                . $this->renderItems($item['items'], $submenuOptions);
            Html::addCssClass($itemOptions, ['widget' => 'dropdown-submenu']);
        }
        $lines[] = Html::tag('li', $content, $itemOptions);
    }
    return Html::tag('ul', implode("\n", $lines), $options);
}

            
run() public method

Renders the widget.

public void run ( )

                public function run()
{
    BootstrapPluginAsset::register($this->getView());
    $this->registerClientEvents();
    return $this->renderItems($this->items, $this->options);
}