Trait yii\bootstrap4\BootstrapWidgetTrait

Implemented byyii\bootstrap4\Accordion, yii\bootstrap4\Alert, yii\bootstrap4\Breadcrumbs, yii\bootstrap4\Button, yii\bootstrap4\ButtonDropdown, yii\bootstrap4\ButtonGroup, yii\bootstrap4\ButtonToolbar, yii\bootstrap4\Carousel, yii\bootstrap4\Dropdown, yii\bootstrap4\InputWidget, yii\bootstrap4\LinkPager, yii\bootstrap4\Modal, yii\bootstrap4\Nav, yii\bootstrap4\NavBar, yii\bootstrap4\Progress, yii\bootstrap4\Tabs, yii\bootstrap4\Toast, yii\bootstrap4\ToggleButtonGroup, yii\bootstrap4\Widget
Source Code https://github.com/yiisoft/yii2-bootstrap4/blob/master/src/BootstrapWidgetTrait.php

BootstrapWidgetTrait is the trait, which provides basic for all bootstrap widgets features.

Note: class, which uses this trait must declare public field named options with the array default value:

class MyWidget extends \yii\base\Widget
{
    use BootstrapWidgetTrait;

    public $options = [];
}

This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict.

Public Properties

Hide inherited properties

Property Type Description Defined By
$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

Public Methods

Hide inherited methods

Method Description Defined By
getView() yii\bootstrap4\BootstrapWidgetTrait
init() Initializes the widget. yii\bootstrap4\BootstrapWidgetTrait

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

Property Details

Hide inherited properties

$clientEvents public property

The event handlers for the underlying Bootstrap JS plugin. Please refer to the corresponding Bootstrap plugin Web page for possible events. For example, this page shows how to use the "Modal" plugin and the supported events (e.g. "shown").

public array $clientEvents = []
$clientOptions public property

The options for the underlying Bootstrap JS plugin. Please refer to the corresponding Bootstrap plugin Web page for possible options. For example, this page shows how to use the "Modal" plugin and the supported options (e.g. "remote").

public array $clientOptions = []

Method Details

Hide inherited methods

getView() public abstract method

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->options['id'])) {
        $this->options['id'] = $this->getId();
    }
}

            
registerClientEvents() protected method

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

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