Browse Source

Better fix for Dropdown container options

Better fix for #3443 that does not break BC and allows widget to be extensible.
tags/2.0.0-rc
Kartik Visweswaran 10 years ago
parent
commit
b74f3b0d65
  1. 17
      Dropdown.php

17
Dropdown.php

@ -40,6 +40,11 @@ class Dropdown extends Widget
*/ */
public $encodeLabels = true; public $encodeLabels = true;
/**
* @var array the HTML attributes for the widget container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
protected $_containerOptions = [];
/** /**
* Initializes the widget. * Initializes the widget.
@ -49,6 +54,7 @@ class Dropdown extends Widget
{ {
parent::init(); parent::init();
Html::addCssClass($this->options, 'dropdown-menu'); Html::addCssClass($this->options, 'dropdown-menu');
$this->_containerOptions = $this->options;
} }
/** /**
@ -56,17 +62,16 @@ class Dropdown extends Widget
*/ */
public function run() public function run()
{ {
echo $this->renderItems($this->items, $this->options); echo $this->renderItems($this->items);
} }
/** /**
* Renders menu items. * Renders menu items.
* @param array $items the menu items to be rendered * @param array $items the menu items to be rendered
* @param array $containerOptions the HTML attributes for the widget container tag
* @return string the rendering result. * @return string the rendering result.
* @throws InvalidConfigException if the label option is not specified in one of the items. * @throws InvalidConfigException if the label option is not specified in one of the items.
*/ */
protected function renderItems($items, $containerOptions) protected function renderItems($items)
{ {
$lines = []; $lines = [];
foreach ($items as $i => $item) { foreach ($items as $i => $item) {
@ -88,13 +93,13 @@ class Dropdown extends Widget
$linkOptions['tabindex'] = '-1'; $linkOptions['tabindex'] = '-1';
$content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions); $content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions);
if (!empty($item['items'])) { if (!empty($item['items'])) {
unset($containerOptions['id']); unset($this->_containerOptions['id']);
$this->renderItems($item['items'], $containerOptions); $this->renderItems($item['items']);
Html::addCssClass($options, 'dropdown-submenu'); Html::addCssClass($options, 'dropdown-submenu');
} }
$lines[] = Html::tag('li', $content, $options); $lines[] = Html::tag('li', $content, $options);
} }
return Html::tag('ul', implode("\n", $lines), $this->options); return Html::tag('ul', implode("\n", $lines), $this->_containerOptions);
} }
} }

Loading…
Cancel
Save