I am trying to display a dropdown-menu on the 2nd-level in the Concrete5 auto-navigation template, but it does not seem to work.
I use the following code:
<?php defined('C5_EXECUTE') or die("Access Denied."); ?>
<?php View::getInstance()->requireAsset('javascript', 'jquery');
$navItems = $controller->getNavItems();
foreach ($navItems as $ni) {
$classes = array();
if ($ni->isCurrent) {
$classes[] = 'nav-selected'; }
if ($ni->inPath) {
$classes[] = 'nav-path-selected';}
if ($ni->hasSubmenu) {
$classes[] = 'dropdown-menu';}
}
echo '<ul class="nav navbar-nav navbar-right">';
foreach ($navItems as $ni) {
echo '<li class="' . $ni->classes . '">';
if ($ni->isEnabled) {
$ni->hasSubmenu;
}
if ($ni->hasSubmenu && $ni->level >= 2 ) {
echo '<a href="#" class="dropdown-toggle dropdown-lvl2" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' . $ni->name . '<span class="caret"></span></a>';
} elseif ($ni->hasSubmenu){
echo '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' . $ni->name . '<span class="caret"></span></a>'; //opens a dropdown sub-menu
} else {
echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '">' . $ni->name . '</a>';
}
if ($ni->hasSubmenu && $ni->level >= 2 ) {
echo '<ul class="dropdown-menu dropdown-menu-lvl2">';
} elseif ($ni->hasSubmenu){
echo '<ul class="dropdown-menu">';
} else {
echo '</li>'; //closes a nav item
echo str_repeat('</ul></li>', $ni->subDepth);
}
}
echo '</ul>'; //closes the top-level menu
So I think the problem is the Bootstrap data-toggle
opening and closing the dropdown-menu
. Any Idea how to make the dropdown work on the other levels.
I found a way to display the multilevel dropdown-menu. I added the following JavaScript:
https://jsfiddle.net/fxv214z0/
<script>
(function($){
$(document).ready(function(){
$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
event.preventDefault();
event.stopPropagation();
$(this).parent().siblings().removeClass('open');
$(this).parent().toggleClass('open');
});
});
})(jQuery);
</script>