Class yii\jui\Accordion
Inheritance | yii\jui\Accordion » yii\jui\Widget » yii\base\Widget |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-jui/blob/master/Accordion.php |
Accordion renders an accordion jQuery UI widget.
For example:
echo Accordion::widget([
'items' => [
[
'header' => 'Section 1',
'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
],
[
'header' => 'Section 2',
'headerOptions' => ['tag' => 'h3'],
'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...',
'options' => ['tag' => 'div'],
],
],
'options' => ['tag' => 'div'],
'itemOptions' => ['tag' => 'div'],
'headerOptions' => ['tag' => 'h3'],
'clientOptions' => ['collapsible' => false],
]);
See also http://api.jqueryui.com/accordion/.
Public 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 |
$headerOptions | array | List of HTML attributes for the item header container tags. | yii\jui\Accordion |
$itemOptions | array | List of HTML attributes for the item container tags. | yii\jui\Accordion |
$items | array | List of collapsible items. | yii\jui\Accordion |
$options | array | The HTML attributes for the widget container tag. | yii\jui\Accordion |
Public Methods
Method | Description | Defined By |
---|---|---|
init() | Initializes the widget. | yii\jui\Widget |
run() | Renders the widget. | yii\jui\Accordion |
Protected 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 collapsible items as specified on $items. | yii\jui\Accordion |
Property Details
List of HTML attributes for the item header container tags. This will be overwritten by the "headerOptions" set in individual $items. The following special options are recognized:
- tag: string, defaults to "h3", the tag name of the item container tags.
See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
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.
List of collapsible items. Each item can be an array of the following structure:
[
'header' => 'Item header',
'content' => 'Item content',
// the HTML attributes of the item header container tag. This will overwrite "headerOptions".
'headerOptions' => [],
// the HTML attributes of the item container tag. This will overwrite "itemOptions".
'options' => [],
]
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.
Method Details
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();
}
}
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));
}
}
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);
}
}
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 |
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);
}
Renders collapsible items as specified on $items.
protected string renderItems ( ) | ||
return | string |
The rendering result. |
---|
protected function renderItems()
{
$items = [];
foreach ($this->items as $item) {
if (!array_key_exists('header', $item)) {
throw new InvalidConfigException("The 'header' option is required.");
}
if (!array_key_exists('content', $item)) {
throw new InvalidConfigException("The 'content' option is required.");
}
$headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', []));
$headerTag = ArrayHelper::remove($headerOptions, 'tag', 'h3');
$items[] = Html::tag($headerTag, $item['header'], $headerOptions);
$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
$tag = ArrayHelper::remove($options, 'tag', 'div');
$items[] = Html::tag($tag, $item['content'], $options);
}
return implode("\n", $items);
}
Renders the widget.
public void run ( ) |
public function run()
{
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'div');
echo Html::beginTag($tag, $options) . "\n";
echo $this->renderItems() . "\n";
echo Html::endTag($tag) . "\n";
$this->registerWidget('accordion');
}