Class yii\jui\Tabs

Inheritanceyii\jui\Tabs » yii\jui\Widget » yii\base\Widget
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-jui/blob/master/Tabs.php

Tabs renders a tabs jQuery UI widget.

For example:

echo Tabs::widget([
    'items' => [
        [
            'label' => 'Tab one',
            'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
        ],
        [
            'label' => 'Tab two',
            'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...',
            'options' => ['tag' => 'div'],
            'headerOptions' => ['class' => 'my-class'],
        ],
        [
            'label' => 'Tab with custom id',
            'content' => 'Morbi tincidunt, dui sit amet facilisis feugiat...',
            'options' => ['id' => 'my-tab'],
        ],
        [
            'label' => 'Ajax tab',
            'url' => ['ajax/content'],
        ],
    ],
    'options' => ['tag' => 'div'],
    'itemOptions' => ['tag' => 'div'],
    'headerOptions' => ['class' => 'my-class'],
    'clientOptions' => ['collapsible' => false],
]);

See also http://api.jqueryui.com/tabs/.

Public Properties

Hide inherited properties

Property Type Description Defined By
$clientEventMap array Event names mapped to what should be specified in .on(). yii\jui\Widget
$clientEvents array The event handlers for the underlying jQuery UI widget. yii\jui\Widget
$clientOptions array The options for the underlying jQuery UI widget. yii\jui\Widget
$encodeLabels boolean Whether the labels for header items should be HTML-encoded. yii\jui\Tabs
$headerOptions array List of HTML attributes for the header container tags. yii\jui\Tabs
$itemOptions array List of HTML attributes for the item container tags. yii\jui\Tabs
$items array List of tab items. yii\jui\Tabs
$linkTemplate string The default header template to render the link. yii\jui\Tabs
$options array The HTML attributes for the widget container tag. yii\jui\Tabs

Protected Properties

Hide inherited properties

Property Type Description Defined By

Public Methods

Hide inherited methods

Method Description Defined By
init() Initializes the widget. yii\jui\Widget
run() Renders the widget. yii\jui\Tabs

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers a specific jQuery UI widget events yii\jui\Widget
registerClientOptions() Registers a specific jQuery UI widget options yii\jui\Widget
registerWidget() Registers a specific jQuery UI widget asset bundle, initializes it with client options and registers related events yii\jui\Widget
renderItems() Renders tab items as specified on $items. yii\jui\Tabs

Property Details

Hide inherited properties

$encodeLabels public property

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

public boolean $encodeLabels true
$headerOptions public property

List of HTML attributes for the header container tags. This will be overwritten by the "headerOptions" set in individual $items.

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

public array $headerOptions = []
$itemOptions public property

List of HTML attributes for the item container tags. This will be overwritten by the "options" set in individual $items. The following special options are recognized:

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

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

public array $itemOptions = []
$items public property

List of tab items. Each item can be an array of the following structure:

  • label: string, required, specifies the header link label. When $encodeLabels is true, the label will be HTML-encoded.
  • content: string, the content to show when corresponding tab is clicked. Can be omitted if url is specified.
  • url: mixed, mixed, optional, the url to load tab contents via AJAX. It is required if no content is specified.
  • template: string, optional, the header link template to render the header link. If none specified $linkTemplate will be used instead.
  • options: array, optional, the HTML attributes of the header.
  • headerOptions: array, optional, the HTML attributes for the header container tag.
public array $items = []
$linkTemplate public property

The default header template to render the link.

public string $linkTemplate '<a href="{url}">{label}</a>'
$options public property

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

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

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

public array $options = []

Method Details

Hide inherited methods

init() public method

Defined in: yii\jui\Widget::init()

Initializes the widget.

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

Defined in: yii\jui\Widget::registerClientEvents()

Registers a specific jQuery UI widget events

protected void registerClientEvents ( $name, $id )
$name string

The name of the jQuery UI widget

$id string

The ID of the widget

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

            
registerClientOptions() protected method

Defined in: yii\jui\Widget::registerClientOptions()

Registers a specific jQuery UI widget options

protected void registerClientOptions ( $name, $id )
$name string

The name of the jQuery UI widget

$id string

The ID of the widget

                protected function registerClientOptions($name, $id)
{
    if ($this->clientOptions !== false) {
        $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
        $js = "jQuery('#$id').$name($options);";
        $this->getView()->registerJs($js);
    }
}

            
registerWidget() protected method

Defined in: yii\jui\Widget::registerWidget()

Registers a specific jQuery UI widget asset bundle, initializes it with client options and registers related events

protected void registerWidget ( $name, $id null )
$name string

The name of the jQuery UI widget

$id string

The ID of the widget. If null, it will use the id value of $options.

                protected function registerWidget($name, $id = null)
{
    if ($id === null) {
        $id = $this->options['id'];
    }
    JuiAsset::register($this->getView());
    $this->registerClientEvents($name, $id);
    $this->registerClientOptions($name, $id);
}

            
renderItems() protected method

Renders tab items as specified on $items.

protected string renderItems ( )
return string

The rendering result.

                protected function renderItems()
{
    $headers = [];
    $items = [];
    foreach ($this->items as $n => $item) {
        if (!isset($item['label'])) {
            throw new InvalidConfigException("The 'label' option is required.");
        }
        if (isset($item['url'])) {
            $url = Url::to($item['url']);
        } else {
            if (!array_key_exists('content', $item)) {
                throw new InvalidConfigException("Either the 'content' or 'url' option is required.");
            }
            $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
            $tag = ArrayHelper::remove($options, 'tag', 'div');
            if (!isset($options['id'])) {
                $options['id'] = $this->options['id'] . '-tab' . $n;
            }
            $url = '#' . $options['id'];
            $items[] = Html::tag($tag, $item['content'], $options);
        }
        $headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', []));
        $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
        $headers[] = Html::tag('li', strtr($template, [
            '{label}' => $this->encodeLabels ? Html::encode($item['label']) : $item['label'],
            '{url}' => $url,
        ]), $headerOptions);
    }
    return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items);
}

            
run() public method

Renders the widget.

public void run ( )

                public function run()
{
    $options = $this->options;
    $tag = ArrayHelper::remove($options, 'tag', 'div');
    $out = Html::beginTag($tag, $options) . "\n";
    $out .= $this->renderItems() . "\n";
    $out .= Html::endTag($tag) . "\n";
    $this->registerWidget('tabs');
    return $out;
}