From d34983a845a00441c1945d2ad3342c5067e8c859 Mon Sep 17 00:00:00 2001 From: Thiago Talma Date: Thu, 17 Jul 2014 17:07:33 -0300 Subject: [PATCH] Option to render the subitems as a Dropdown (or not) --- extensions/bootstrap/Nav.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/extensions/bootstrap/Nav.php b/extensions/bootstrap/Nav.php index 3a5791f2e5e..7b8f0db945b 100644 --- a/extensions/bootstrap/Nav.php +++ b/extensions/bootstrap/Nav.php @@ -64,6 +64,10 @@ class Nav extends Widget * If a menu item is a string, it will be rendered directly without HTML encoding. */ public $items = []; + /** + * @var boolean whether the nav subitems should be a [[Dropdown]] widget. + */ + public $asDropdown = true; /** * @var boolean whether the nav items labels should be HTML-encoded. */ @@ -113,25 +117,25 @@ public function init() */ public function run() { - echo $this->renderItems(); + echo $this->renderItems($this->items, $this->options); BootstrapAsset::register($this->getView()); } /** * Renders widget items. */ - public function renderItems() + public function renderItems($items, $options = []) { - $items = []; - foreach ($this->items as $i => $item) { + $rederedItems = []; + foreach ($items as $i => $item) { if (isset($item['visible']) && !$item['visible']) { unset($items[$i]); continue; } - $items[] = $this->renderItem($item); + $rederedItems[] = $this->renderItem($item); } - return Html::tag('ul', implode("\n", $items), $this->options); + return Html::tag('ul', implode("\n", $rederedItems), $options); } /** @@ -162,20 +166,26 @@ public function renderItem($item) } if ($items !== null) { + if ($this->asDropdown) { $linkOptions['data-toggle'] = 'dropdown'; Html::addCssClass($options, 'dropdown'); Html::addCssClass($linkOptions, 'dropdown-toggle'); $label .= ' ' . Html::tag('b', '', ['class' => 'caret']); + } if (is_array($items)) { if ($this->activateItems) { $items = $this->isChildActive($items, $active); } + if ($this->asDropdown) { $items = Dropdown::widget([ 'items' => $items, 'encodeLabels' => $this->encodeLabels, 'clientOptions' => false, 'view' => $this->getView(), ]); + } else { + $items = $this->renderItems($items); + } } }