How to make dynamic dropdown menu? - angularjs

<ul class="nav navbar-nav friend-label">
<li class="dropdown" ng-repeat="module in modules">
<a href="" class="dropdown-toggle" data-toggle="dropdown">
{{module.ModuleName}} <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li ng-repeat="subModule in module.SubModules">
<a href=""><b>{{subModule.SubModuleName}} </b>
</a>
</li>
</ul>
</li>
</ul>
By which, I'm getting following out put.
Question and problem is, when I do have submenu its fine if dropdown appears,
but when there is no submenu, dropdown should not come. still it shows dropdown small pointer arrow. and if you click it, it comes out with blank dropdown.
As shown in this fig, Contact Us menu doesn't have submenu. Still blank dropdown comes. I want to stop it using above code of HTML. Its a small problem. but I'm unable to fig it out.

Using ngRepeat you can dynamically populated menu from the model:
<span class="dropdown" dropdown on-toggle="toggled(open)">
<a href class="dropdown-toggle" dropdown-toggle>
Click me!
</a>
<ul class="dropdown-menu">
<li ng-repeat="choice in items">
<a ng-click="runFn(choice.fn)">{{choice.name}}</a>
</li>
</ul>
</span>
Look for fully worked example on Plunker

I am inferring that you are populating your dropdown items dynamically via ajax or server-side.
If that is true, then you could make use of small CSS/Javascript to hide empty menus. But, you have to take care of a few quirks.
First make sure that your markup looks like this:
<li class="dropdown">
Contact Us <span class="caret"></span>
<ul class="dropdown-menu" role="menu"></ul>
</li>
Important: Notice, that there is no white space between your ul.dropdown-menu start and end tags. This is because, the CSS :empty selector will fail if there is a white-space or line-break.
Now, if there are menu-items in your database you could populate those inside the ul.dropdown-menu as li. If there are no items, then it will left empty. At this point you will have a small dropdown which is empty and still a caret showing.
A little CSS can make the empty dropdown go away:
ul.dropdown-menu:empty {
display: none;
}
But, because CSS does not have reverse adjacent sibling combinator, we cannot target the caret. So, we have to resort to a little Javascript/jQuery:
$("ul.dropdown-menu:empty").prev("a").children("span.caret").first().hide();
You can choose to have both implemented only in jQuery if you wish to. This is just to give you an idea.
Put together your setup now looks and behaves something like this snippet below. Please view it in full-screen to prevent collapsing.
Snippet:
$("ul.dropdown-menu:empty").prev("a").children("span.caret").first().hide();
ul.dropdown-menu:empty {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<nav class="navbar navbar-default" role="navigation">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
</ul>
</li>
<li class="dropdown">
Contact Us <span class="caret"></span>
<ul class="dropdown-menu" role="menu"></ul>
</li>
</ul>
</nav>

Related

open dropdown with ng-class Angularjs and typescript

I need to open the dropdown when clicking on link with angularjs. my idea is to add the class open to the dropdown with ng-class directive but this is not working .
I have two dropdown menu, what i need is to open the second one when clicking on the link within the first one
<ul class="nav navbar-nav navbar-right " >
<li class="dropdown" ng-class="myDropdown ? : 'open'">
<a class="dropdown-toggle" data-toggle="dropdown" >Menu</a>
<ul class="dropdown-menu " role="menu" ng-if="!myDropdown">
<li>
Open second dropdown
</li>
</ul>
<ul class="dropdown-menu " role="menu" ng-if="myDropdown">
<li >
Go back
</li>
</ul>
</li>
</ul>
Typescript
toogleDropDown() {
this.scope.myDropdown = true;
}
This won't work since you toggleDropDown is a function and it does not return anything !undefined should be true I think so your line:
Go back
is same as writing:
Go back
overall there's no need for that function at all what you need is something like this:
<a href="" ng-click="myDropdown=!myDropdown">
<span ng-if="!myDropdown">Open second dropdown</span>
<span ng-if="myDropdown">Go back</span>
</a>
and remove that function from your controller there's no need for it.
also I believe you wanted to set up ng-class like this:
<li class="dropdown" ng-class="{'open': myDropdown}">
Here's fiddle: https://jsfiddle.net/pegla/nq11093m/1/

Angular bootstrap dropdown auto-close not working when set to outside click

It is a simple dropdown which is supposed to close only on outside click.
It used to work in the earlier version (0.13).
Am I doing something wrong.
Don't want to have a work around like stop propagation and prevent default. That is my last resort.
Below is the plunkr for a simple dropdown:
http://plnkr.co/edit/A2vDHb43PThalcHAhLkJ?p=preview
<span class="dropdown" uib-dropdown auto-close="outsideClick">
<a href="javascript:void(0);" class="dropdown-toggle" uib-dropdown-toggle>
Click me for a dropdown, yo!
</a>
<ul class="dropdown-menu">
<li>Element 1</li>
<li>Element 2</li>
</ul>
</span>
You should use uib-dropdown-menu directive:
<ul uib-dropdown-menu class="dropdown-menu">
<li>Element 1</li>
<li>Element 2</li>
</ul>
Here's a working plunker

How to combine navbar, dropdown and collapse in AngularStrap

I have a quite typical scenario:
AngularJS (currently 1.2, want to upgrade to 1.3/1.4 soon)
Bootstrap 3.x
AngularStrap 2.1.x
As top-level menu I place a navbar with some dropdown menus and I'd like it to be collapsable for smaller devices (tablet portrait, phone).
<div class="navbar navbar-inverse navbar-fixed-top"
ng-controller="MenuCtrl" bs-navbar>
<div class="container-fluid" bs-collapse start-collapsed="true">
<div class="navbar-header">
<button type="button" class="navbar-toggle"
bs-collapse-toggle>
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">ACME</a>
</div>
<div class="navbar-collapse collapse" bs-collapse-target>
<ul class="nav navbar-nav navbar-right">
<li>Home</li>
<li class="dropdown" bs-dropdown>
<a href="#" class="dropdown-toggle"
data-toggle="dropdown"
id="tst-dropdown-session">
Session <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>Login</li>
<li>Logout</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
(Of course above code is reduced compared to my real code)
The problem is: when clicking on the top-menu item, only a small dropdown shows up (~10px height) without sub-menu items.
Does anybody have a working example for this?
I could manage the menu items in a model of the controller, but how does "collapse" work then? Any ideas?
Edit (add pictures)
On narrow devices it should not look like this:
But it should more like this:
Define your menu items in controller.
And use it your html template as follows
HTML code
<li>Home</li>
<li class="dropdown" bs-dropdown="dropdownHtml">
<a href="#" class="dropdown-toggle"
id="tst-dropdown-session">
Session <b class="caret"></b>
</a>
</li>
Controller code
$scope.dropdownHtml = [
{
text: "Login",
href: '#/login',
title: 'Log out'
},
{
text: "Logout",
href: '/api/auth/logout',
title: 'Log out'
}
];

ui-sref-active in dropdown

I am using below code
<li class="dropdown" ui-sref-active="activeclassroot">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li ui-sref-active="activeclasschild"><a ui-sref="products1" >products1</a></li>
<li ui-sref-active="activeclasschild"><a ui-sref="products2" >products2</a></li>
</ul>
</li>
Here activeclasschild class is getting applied successfully. Is there any way to apply activeclassroot class to the root li if any of the child links are active?

Angular UI multi-level Dropdown Toggle

I'm trying to use the Angular UI Dropdown Toggle to create a multi-level / nested drop down.
When I click on the first level of the dropdown it simply closes up altogether and does not reveal the second tier.
I've attached a very basic plunkr to demonstrate a bare bones version of what I'm trying to achieve.
http://plnkr.co/edit/c0GbKQoYWICXwd3JRWGV?p=info
Any help, greatly appreciated.
Thanks
Sub-menu has been removed from Boostrap 3 as it is deemed irrelevant for mobile.
"Submenus just don't have much of a place on the web right now, especially the mobile web. They will be removed with 3.0" - https://github.com/twbs/bootstrap/pull/6342
An example that uses Bootstrap 3.0.0 (final): http://bootply.com/86684
Code from StackOverflow post:
Bootstrap 3 dropdown sub menu missing
You can use the class "dropdown-submenu" to achieve this.
<div class="btn-group dropdown">
<button class="dropdown-toggle">Toggle</button>
<ul class="dropdown-menu">
<li>Item 1</li>
<li>Item 2</li>
<li class="dropdown-submenu">
Sub List
<ul class="dropdown-menu">
<li>Submenu Item 1</li>
<li>Submenu Item 2</li>
</ul>
</li>
</ul>
</div>
You can use the ng-bootstrap-submenu module to get the sub-menu you're looking for.
https://www.npmjs.com/package/ng-bootstrap-submenu
Here is a plnkr demonstrating it's usage.
<nav class="navbar navbar-default">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand">ng-bootstrap-submenu</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<bootstrap-submenu ng-repeat="item in menuItems" menu-item="item">
</bootstrap-submenu>
</ul>
</div>
</nav>

Resources