Skip to content

Instantly share code, notes, and snippets.

@jgdovin
Created April 13, 2015 15:35
Show Gist options
  • Save jgdovin/ac2bd1f03f2f50636ce0 to your computer and use it in GitHub Desktop.
Save jgdovin/ac2bd1f03f2f50636ce0 to your computer and use it in GitHub Desktop.
<head>
<title>dropdown</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
{{> main}}
</body>
<template name="main">
<div class="container">
<div class="row">
<h2>Multi level dropdown menu in Bootstrap 3 and Meteor Collection</h2>
<hr>
<div class="dropdown">
<a id="dLabel" role="button" data-toggle="dropdown" class="btn btn-primary" data-target="#">
Dropdown <span class="caret"></span>
</a>
<ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
{{#each menuItems}}
{{> menuItemsTemplate}}
{{/each}}
</ul>
</div>
</div>
</div>
</template>
<template name="menuItemsTemplate">
{{#if hasChildren}}
<li class="dropdown-submenu">
<a href="#" tabindex="-1" data-id="{{_id}}">{{name}}</a>
<ul class="dropdown-menu">
{{#each children}}
{{>menuItemsTemplate}}
{{/each}}
</ul>
</li>
{{else}}
<li><a href="#" data-id="{{_id}}">{{name}}</a></li>
{{/if}}
</template>
if (Meteor.isClient) {
Meteor.subscribe('menuItems');
// counter starts at 0
Session.setDefault('counter', 0);
Template.main.helpers({
menuItems : function() {
return menuItems.find({parentId : null});
}
});
Template.menuItemsTemplate.helpers({
hasChildren: function() {
return menuItems.find({parentId : this._id}).count() > 0;
},
children: function() {
return menuItems.find({parentId: this._id});
}
});
Template.main.events({
});
}
if (Meteor.isServer) {
Meteor.startup(function() {
if(menuItems.find().count() === 0) {
var firstMenu = menuItems.insert({name: 'Top Level'});
var secondLevel = menuItems.insert({name: 'Second Level 1', parentId: firstMenu});
var thirdLevel = menuItems.insert({name: 'Third Level 1', parentId: secondLevel});
var fourthLevel = menuItems.insert({name: 'Fourth Level', parentId: thirdLevel});
var fifthLevel = menuItems.insert({name: 'This is getting ridiculous', parentId: fourthLevel});
var secondLevel2 = menuItems.insert({name: 'Second Level 2', parentId: firstMenu});
var singleLevel = menuItems.insert({name: 'Top Level again'});
}
});
Meteor.publish('menuItems');
}
menuItems = new Mongo.Collection('menuItems');
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
-webkit-border-radius: 0 6px 6px 6px;
-moz-border-radius: 0 6px 6px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #ccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #fff;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
-webkit-border-radius: 6px 0 6px 6px;
-moz-border-radius: 6px 0 6px 6px;
border-radius: 6px 0 6px 6px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment