How to create Bootstrap4 navbar with menus and sub menus with JSON data?
I have a JSON string from which I want to create a Bootstrap 4 navbar. As I am new to JavaScript and jQuery I am not getting an idea of how to achieve that.
Here is my JSON data:
var data = {
"Setup":
[
{ "type": "Submenu-1" },
{ "type": "submenu-2" }
],
"Report":
[
{ "type": "subreport-1" },
{ "type": "subreport-2" }
],
"logout":
};
I want to have a navbar like
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<!-- Links -->
<!-- DropDown -->
<!-- setup -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbardrop"
data-toggle="dropdown"> Setup </a>
<div id="drop" class="dropdown-menu">
<a class="dropdown-item" href="#"> submenu-1</a>
<a class="dropdown-item" href="#"> submenu-2</a>
</div></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop"
data-toggle="dropdown"> Report </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="">subreport-1</a>
<a class="dropdown-item" href="">subreport-2</a>
</div></li>
<li class="nav-item">
<a class="nav-link" href="">Logout</a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
I don't know how I can achieve this from JavaScript or jQuery - how can I achieve this?
My JSON data is dynamic; it can vary from 3 menus to 6-7, and the same for submenus also.
javascript jquery html bootstrap-4
add a comment |
I have a JSON string from which I want to create a Bootstrap 4 navbar. As I am new to JavaScript and jQuery I am not getting an idea of how to achieve that.
Here is my JSON data:
var data = {
"Setup":
[
{ "type": "Submenu-1" },
{ "type": "submenu-2" }
],
"Report":
[
{ "type": "subreport-1" },
{ "type": "subreport-2" }
],
"logout":
};
I want to have a navbar like
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<!-- Links -->
<!-- DropDown -->
<!-- setup -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbardrop"
data-toggle="dropdown"> Setup </a>
<div id="drop" class="dropdown-menu">
<a class="dropdown-item" href="#"> submenu-1</a>
<a class="dropdown-item" href="#"> submenu-2</a>
</div></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop"
data-toggle="dropdown"> Report </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="">subreport-1</a>
<a class="dropdown-item" href="">subreport-2</a>
</div></li>
<li class="nav-item">
<a class="nav-link" href="">Logout</a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
I don't know how I can achieve this from JavaScript or jQuery - how can I achieve this?
My JSON data is dynamic; it can vary from 3 menus to 6-7, and the same for submenus also.
javascript jquery html bootstrap-4
@adiga i have tried this " for (var i in data) { $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>'); var div = '<div role="tabpanel" class="tab-pane" id="' + i + '">'; for (var j = 0; j < data[i].length; j++) { var obj = data[i][j]; div += '<div id="' + obj.id + '">' + obj.type + '</div>'; } $('.tab-content').append(div); }" i ahve found this one from google only but its only working for ul tag and not for nav
– dheeraj kumar
Nov 21 '18 at 10:36
Please see this example: jsfiddle.net/saslam/vepeLmm0
– Farhad Mortezapour
Nov 21 '18 at 10:37
@FarhadMortezapour its not talkin bootstrap cl;asses and not even form the toggle button when used for small device
– manish thakur
Nov 21 '18 at 11:17
@FarhadMortezapour that was not helpfull at all ;(
– manish thakur
Nov 21 '18 at 11:39
add a comment |
I have a JSON string from which I want to create a Bootstrap 4 navbar. As I am new to JavaScript and jQuery I am not getting an idea of how to achieve that.
Here is my JSON data:
var data = {
"Setup":
[
{ "type": "Submenu-1" },
{ "type": "submenu-2" }
],
"Report":
[
{ "type": "subreport-1" },
{ "type": "subreport-2" }
],
"logout":
};
I want to have a navbar like
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<!-- Links -->
<!-- DropDown -->
<!-- setup -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbardrop"
data-toggle="dropdown"> Setup </a>
<div id="drop" class="dropdown-menu">
<a class="dropdown-item" href="#"> submenu-1</a>
<a class="dropdown-item" href="#"> submenu-2</a>
</div></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop"
data-toggle="dropdown"> Report </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="">subreport-1</a>
<a class="dropdown-item" href="">subreport-2</a>
</div></li>
<li class="nav-item">
<a class="nav-link" href="">Logout</a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
I don't know how I can achieve this from JavaScript or jQuery - how can I achieve this?
My JSON data is dynamic; it can vary from 3 menus to 6-7, and the same for submenus also.
javascript jquery html bootstrap-4
I have a JSON string from which I want to create a Bootstrap 4 navbar. As I am new to JavaScript and jQuery I am not getting an idea of how to achieve that.
Here is my JSON data:
var data = {
"Setup":
[
{ "type": "Submenu-1" },
{ "type": "submenu-2" }
],
"Report":
[
{ "type": "subreport-1" },
{ "type": "subreport-2" }
],
"logout":
};
I want to have a navbar like
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<!-- Links -->
<!-- DropDown -->
<!-- setup -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbardrop"
data-toggle="dropdown"> Setup </a>
<div id="drop" class="dropdown-menu">
<a class="dropdown-item" href="#"> submenu-1</a>
<a class="dropdown-item" href="#"> submenu-2</a>
</div></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop"
data-toggle="dropdown"> Report </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="">subreport-1</a>
<a class="dropdown-item" href="">subreport-2</a>
</div></li>
<li class="nav-item">
<a class="nav-link" href="">Logout</a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
I don't know how I can achieve this from JavaScript or jQuery - how can I achieve this?
My JSON data is dynamic; it can vary from 3 menus to 6-7, and the same for submenus also.
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<!-- Links -->
<!-- DropDown -->
<!-- setup -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbardrop"
data-toggle="dropdown"> Setup </a>
<div id="drop" class="dropdown-menu">
<a class="dropdown-item" href="#"> submenu-1</a>
<a class="dropdown-item" href="#"> submenu-2</a>
</div></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop"
data-toggle="dropdown"> Report </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="">subreport-1</a>
<a class="dropdown-item" href="">subreport-2</a>
</div></li>
<li class="nav-item">
<a class="nav-link" href="">Logout</a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<!-- Links -->
<!-- DropDown -->
<!-- setup -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbardrop"
data-toggle="dropdown"> Setup </a>
<div id="drop" class="dropdown-menu">
<a class="dropdown-item" href="#"> submenu-1</a>
<a class="dropdown-item" href="#"> submenu-2</a>
</div></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop"
data-toggle="dropdown"> Report </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="">subreport-1</a>
<a class="dropdown-item" href="">subreport-2</a>
</div></li>
<li class="nav-item">
<a class="nav-link" href="">Logout</a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
javascript jquery html bootstrap-4
javascript jquery html bootstrap-4
edited Dec 29 '18 at 21:51
halfer
14.3k758109
14.3k758109
asked Nov 21 '18 at 10:27
manish thakur
38512
38512
@adiga i have tried this " for (var i in data) { $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>'); var div = '<div role="tabpanel" class="tab-pane" id="' + i + '">'; for (var j = 0; j < data[i].length; j++) { var obj = data[i][j]; div += '<div id="' + obj.id + '">' + obj.type + '</div>'; } $('.tab-content').append(div); }" i ahve found this one from google only but its only working for ul tag and not for nav
– dheeraj kumar
Nov 21 '18 at 10:36
Please see this example: jsfiddle.net/saslam/vepeLmm0
– Farhad Mortezapour
Nov 21 '18 at 10:37
@FarhadMortezapour its not talkin bootstrap cl;asses and not even form the toggle button when used for small device
– manish thakur
Nov 21 '18 at 11:17
@FarhadMortezapour that was not helpfull at all ;(
– manish thakur
Nov 21 '18 at 11:39
add a comment |
@adiga i have tried this " for (var i in data) { $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>'); var div = '<div role="tabpanel" class="tab-pane" id="' + i + '">'; for (var j = 0; j < data[i].length; j++) { var obj = data[i][j]; div += '<div id="' + obj.id + '">' + obj.type + '</div>'; } $('.tab-content').append(div); }" i ahve found this one from google only but its only working for ul tag and not for nav
– dheeraj kumar
Nov 21 '18 at 10:36
Please see this example: jsfiddle.net/saslam/vepeLmm0
– Farhad Mortezapour
Nov 21 '18 at 10:37
@FarhadMortezapour its not talkin bootstrap cl;asses and not even form the toggle button when used for small device
– manish thakur
Nov 21 '18 at 11:17
@FarhadMortezapour that was not helpfull at all ;(
– manish thakur
Nov 21 '18 at 11:39
@adiga i have tried this " for (var i in data) { $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>'); var div = '<div role="tabpanel" class="tab-pane" id="' + i + '">'; for (var j = 0; j < data[i].length; j++) { var obj = data[i][j]; div += '<div id="' + obj.id + '">' + obj.type + '</div>'; } $('.tab-content').append(div); }" i ahve found this one from google only but its only working for ul tag and not for nav
– dheeraj kumar
Nov 21 '18 at 10:36
@adiga i have tried this " for (var i in data) { $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>'); var div = '<div role="tabpanel" class="tab-pane" id="' + i + '">'; for (var j = 0; j < data[i].length; j++) { var obj = data[i][j]; div += '<div id="' + obj.id + '">' + obj.type + '</div>'; } $('.tab-content').append(div); }" i ahve found this one from google only but its only working for ul tag and not for nav
– dheeraj kumar
Nov 21 '18 at 10:36
Please see this example: jsfiddle.net/saslam/vepeLmm0
– Farhad Mortezapour
Nov 21 '18 at 10:37
Please see this example: jsfiddle.net/saslam/vepeLmm0
– Farhad Mortezapour
Nov 21 '18 at 10:37
@FarhadMortezapour its not talkin bootstrap cl;asses and not even form the toggle button when used for small device
– manish thakur
Nov 21 '18 at 11:17
@FarhadMortezapour its not talkin bootstrap cl;asses and not even form the toggle button when used for small device
– manish thakur
Nov 21 '18 at 11:17
@FarhadMortezapour that was not helpfull at all ;(
– manish thakur
Nov 21 '18 at 11:39
@FarhadMortezapour that was not helpfull at all ;(
– manish thakur
Nov 21 '18 at 11:39
add a comment |
1 Answer
1
active
oldest
votes
I hope it will help to generate dynamic navbar from Json data.
var data = {
"Setup":
[
{ "type": "Submenu-1", "link" : "https://www.google.com" },
{ "type": "submenu-2", "link" : "https://www.google.com" }
],
"Report":
[
{ "type": "subreport-1", "link" : "https://www.google.com" },
{ "type": "subreport-2", "link" : "https://www.google.com" }
],
"logout":
};
$(document).ready(function() {
for (var item in data) {
var _menu = "";
var _submenuData = data[item];
if(_submenuData.length > 0) {
var _submenu = "";
for(var i = 0; i < _submenuData.length; i++) {
_submenu += "<a class='dropdown-item' href='" + _submenuData[i]["link"] + "'>" + _submenuData[i]["type"] + "</a>";
}
_menu = "<li class='nav-item dropdown'>"
+ "<a class='nav-link dropdown-toggle' href='' id='navbardrop' data-toggle='dropdown'> " + item + " </a>"
+ "<div id='drop' class='dropdown-menu'>"
+ _submenu
+ "</div>"
+ "</li>";
}
else {
_menu = "<li class='nav-item'>"
+ "<a class='nav-link' href=''> " + item + " </a>"
+ "</li>";
}
$("#navbarId").append(_menu);
}
});
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul id="navbarId" class="navbar-nav mr-auto">
</ul>
</div>
</nav>
hey it is the solution i was looking for thanxx :)... i have a small question to ask may i ?
– manish thakur
Nov 21 '18 at 12:07
Yes, ask question
– Aquib Iqbal
Nov 21 '18 at 12:09
like in my json currently i am calling only sub menu name ...now what if i have to provide a link to submenu-1...should i have to call that link in my json?..like some one click on submenu then it should go to that link
– manish thakur
Nov 21 '18 at 12:11
I updated the snippet, check it you will get how to add link(url) to sub menus
– Aquib Iqbal
Nov 21 '18 at 12:23
wait ill test it
– manish thakur
Nov 21 '18 at 12:29
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410013%2fhow-to-create-bootstrap4-navbar-with-menus-and-sub-menus-with-json-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I hope it will help to generate dynamic navbar from Json data.
var data = {
"Setup":
[
{ "type": "Submenu-1", "link" : "https://www.google.com" },
{ "type": "submenu-2", "link" : "https://www.google.com" }
],
"Report":
[
{ "type": "subreport-1", "link" : "https://www.google.com" },
{ "type": "subreport-2", "link" : "https://www.google.com" }
],
"logout":
};
$(document).ready(function() {
for (var item in data) {
var _menu = "";
var _submenuData = data[item];
if(_submenuData.length > 0) {
var _submenu = "";
for(var i = 0; i < _submenuData.length; i++) {
_submenu += "<a class='dropdown-item' href='" + _submenuData[i]["link"] + "'>" + _submenuData[i]["type"] + "</a>";
}
_menu = "<li class='nav-item dropdown'>"
+ "<a class='nav-link dropdown-toggle' href='' id='navbardrop' data-toggle='dropdown'> " + item + " </a>"
+ "<div id='drop' class='dropdown-menu'>"
+ _submenu
+ "</div>"
+ "</li>";
}
else {
_menu = "<li class='nav-item'>"
+ "<a class='nav-link' href=''> " + item + " </a>"
+ "</li>";
}
$("#navbarId").append(_menu);
}
});
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul id="navbarId" class="navbar-nav mr-auto">
</ul>
</div>
</nav>
hey it is the solution i was looking for thanxx :)... i have a small question to ask may i ?
– manish thakur
Nov 21 '18 at 12:07
Yes, ask question
– Aquib Iqbal
Nov 21 '18 at 12:09
like in my json currently i am calling only sub menu name ...now what if i have to provide a link to submenu-1...should i have to call that link in my json?..like some one click on submenu then it should go to that link
– manish thakur
Nov 21 '18 at 12:11
I updated the snippet, check it you will get how to add link(url) to sub menus
– Aquib Iqbal
Nov 21 '18 at 12:23
wait ill test it
– manish thakur
Nov 21 '18 at 12:29
|
show 2 more comments
I hope it will help to generate dynamic navbar from Json data.
var data = {
"Setup":
[
{ "type": "Submenu-1", "link" : "https://www.google.com" },
{ "type": "submenu-2", "link" : "https://www.google.com" }
],
"Report":
[
{ "type": "subreport-1", "link" : "https://www.google.com" },
{ "type": "subreport-2", "link" : "https://www.google.com" }
],
"logout":
};
$(document).ready(function() {
for (var item in data) {
var _menu = "";
var _submenuData = data[item];
if(_submenuData.length > 0) {
var _submenu = "";
for(var i = 0; i < _submenuData.length; i++) {
_submenu += "<a class='dropdown-item' href='" + _submenuData[i]["link"] + "'>" + _submenuData[i]["type"] + "</a>";
}
_menu = "<li class='nav-item dropdown'>"
+ "<a class='nav-link dropdown-toggle' href='' id='navbardrop' data-toggle='dropdown'> " + item + " </a>"
+ "<div id='drop' class='dropdown-menu'>"
+ _submenu
+ "</div>"
+ "</li>";
}
else {
_menu = "<li class='nav-item'>"
+ "<a class='nav-link' href=''> " + item + " </a>"
+ "</li>";
}
$("#navbarId").append(_menu);
}
});
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul id="navbarId" class="navbar-nav mr-auto">
</ul>
</div>
</nav>
hey it is the solution i was looking for thanxx :)... i have a small question to ask may i ?
– manish thakur
Nov 21 '18 at 12:07
Yes, ask question
– Aquib Iqbal
Nov 21 '18 at 12:09
like in my json currently i am calling only sub menu name ...now what if i have to provide a link to submenu-1...should i have to call that link in my json?..like some one click on submenu then it should go to that link
– manish thakur
Nov 21 '18 at 12:11
I updated the snippet, check it you will get how to add link(url) to sub menus
– Aquib Iqbal
Nov 21 '18 at 12:23
wait ill test it
– manish thakur
Nov 21 '18 at 12:29
|
show 2 more comments
I hope it will help to generate dynamic navbar from Json data.
var data = {
"Setup":
[
{ "type": "Submenu-1", "link" : "https://www.google.com" },
{ "type": "submenu-2", "link" : "https://www.google.com" }
],
"Report":
[
{ "type": "subreport-1", "link" : "https://www.google.com" },
{ "type": "subreport-2", "link" : "https://www.google.com" }
],
"logout":
};
$(document).ready(function() {
for (var item in data) {
var _menu = "";
var _submenuData = data[item];
if(_submenuData.length > 0) {
var _submenu = "";
for(var i = 0; i < _submenuData.length; i++) {
_submenu += "<a class='dropdown-item' href='" + _submenuData[i]["link"] + "'>" + _submenuData[i]["type"] + "</a>";
}
_menu = "<li class='nav-item dropdown'>"
+ "<a class='nav-link dropdown-toggle' href='' id='navbardrop' data-toggle='dropdown'> " + item + " </a>"
+ "<div id='drop' class='dropdown-menu'>"
+ _submenu
+ "</div>"
+ "</li>";
}
else {
_menu = "<li class='nav-item'>"
+ "<a class='nav-link' href=''> " + item + " </a>"
+ "</li>";
}
$("#navbarId").append(_menu);
}
});
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul id="navbarId" class="navbar-nav mr-auto">
</ul>
</div>
</nav>
I hope it will help to generate dynamic navbar from Json data.
var data = {
"Setup":
[
{ "type": "Submenu-1", "link" : "https://www.google.com" },
{ "type": "submenu-2", "link" : "https://www.google.com" }
],
"Report":
[
{ "type": "subreport-1", "link" : "https://www.google.com" },
{ "type": "subreport-2", "link" : "https://www.google.com" }
],
"logout":
};
$(document).ready(function() {
for (var item in data) {
var _menu = "";
var _submenuData = data[item];
if(_submenuData.length > 0) {
var _submenu = "";
for(var i = 0; i < _submenuData.length; i++) {
_submenu += "<a class='dropdown-item' href='" + _submenuData[i]["link"] + "'>" + _submenuData[i]["type"] + "</a>";
}
_menu = "<li class='nav-item dropdown'>"
+ "<a class='nav-link dropdown-toggle' href='' id='navbardrop' data-toggle='dropdown'> " + item + " </a>"
+ "<div id='drop' class='dropdown-menu'>"
+ _submenu
+ "</div>"
+ "</li>";
}
else {
_menu = "<li class='nav-item'>"
+ "<a class='nav-link' href=''> " + item + " </a>"
+ "</li>";
}
$("#navbarId").append(_menu);
}
});
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul id="navbarId" class="navbar-nav mr-auto">
</ul>
</div>
</nav>
var data = {
"Setup":
[
{ "type": "Submenu-1", "link" : "https://www.google.com" },
{ "type": "submenu-2", "link" : "https://www.google.com" }
],
"Report":
[
{ "type": "subreport-1", "link" : "https://www.google.com" },
{ "type": "subreport-2", "link" : "https://www.google.com" }
],
"logout":
};
$(document).ready(function() {
for (var item in data) {
var _menu = "";
var _submenuData = data[item];
if(_submenuData.length > 0) {
var _submenu = "";
for(var i = 0; i < _submenuData.length; i++) {
_submenu += "<a class='dropdown-item' href='" + _submenuData[i]["link"] + "'>" + _submenuData[i]["type"] + "</a>";
}
_menu = "<li class='nav-item dropdown'>"
+ "<a class='nav-link dropdown-toggle' href='' id='navbardrop' data-toggle='dropdown'> " + item + " </a>"
+ "<div id='drop' class='dropdown-menu'>"
+ _submenu
+ "</div>"
+ "</li>";
}
else {
_menu = "<li class='nav-item'>"
+ "<a class='nav-link' href=''> " + item + " </a>"
+ "</li>";
}
$("#navbarId").append(_menu);
}
});
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul id="navbarId" class="navbar-nav mr-auto">
</ul>
</div>
</nav>
var data = {
"Setup":
[
{ "type": "Submenu-1", "link" : "https://www.google.com" },
{ "type": "submenu-2", "link" : "https://www.google.com" }
],
"Report":
[
{ "type": "subreport-1", "link" : "https://www.google.com" },
{ "type": "subreport-2", "link" : "https://www.google.com" }
],
"logout":
};
$(document).ready(function() {
for (var item in data) {
var _menu = "";
var _submenuData = data[item];
if(_submenuData.length > 0) {
var _submenu = "";
for(var i = 0; i < _submenuData.length; i++) {
_submenu += "<a class='dropdown-item' href='" + _submenuData[i]["link"] + "'>" + _submenuData[i]["type"] + "</a>";
}
_menu = "<li class='nav-item dropdown'>"
+ "<a class='nav-link dropdown-toggle' href='' id='navbardrop' data-toggle='dropdown'> " + item + " </a>"
+ "<div id='drop' class='dropdown-menu'>"
+ _submenu
+ "</div>"
+ "</li>";
}
else {
_menu = "<li class='nav-item'>"
+ "<a class='nav-link' href=''> " + item + " </a>"
+ "</li>";
}
$("#navbarId").append(_menu);
}
});
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul id="navbarId" class="navbar-nav mr-auto">
</ul>
</div>
</nav>
edited Nov 21 '18 at 12:21
answered Nov 21 '18 at 12:04
Aquib Iqbal
21417
21417
hey it is the solution i was looking for thanxx :)... i have a small question to ask may i ?
– manish thakur
Nov 21 '18 at 12:07
Yes, ask question
– Aquib Iqbal
Nov 21 '18 at 12:09
like in my json currently i am calling only sub menu name ...now what if i have to provide a link to submenu-1...should i have to call that link in my json?..like some one click on submenu then it should go to that link
– manish thakur
Nov 21 '18 at 12:11
I updated the snippet, check it you will get how to add link(url) to sub menus
– Aquib Iqbal
Nov 21 '18 at 12:23
wait ill test it
– manish thakur
Nov 21 '18 at 12:29
|
show 2 more comments
hey it is the solution i was looking for thanxx :)... i have a small question to ask may i ?
– manish thakur
Nov 21 '18 at 12:07
Yes, ask question
– Aquib Iqbal
Nov 21 '18 at 12:09
like in my json currently i am calling only sub menu name ...now what if i have to provide a link to submenu-1...should i have to call that link in my json?..like some one click on submenu then it should go to that link
– manish thakur
Nov 21 '18 at 12:11
I updated the snippet, check it you will get how to add link(url) to sub menus
– Aquib Iqbal
Nov 21 '18 at 12:23
wait ill test it
– manish thakur
Nov 21 '18 at 12:29
hey it is the solution i was looking for thanxx :)... i have a small question to ask may i ?
– manish thakur
Nov 21 '18 at 12:07
hey it is the solution i was looking for thanxx :)... i have a small question to ask may i ?
– manish thakur
Nov 21 '18 at 12:07
Yes, ask question
– Aquib Iqbal
Nov 21 '18 at 12:09
Yes, ask question
– Aquib Iqbal
Nov 21 '18 at 12:09
like in my json currently i am calling only sub menu name ...now what if i have to provide a link to submenu-1...should i have to call that link in my json?..like some one click on submenu then it should go to that link
– manish thakur
Nov 21 '18 at 12:11
like in my json currently i am calling only sub menu name ...now what if i have to provide a link to submenu-1...should i have to call that link in my json?..like some one click on submenu then it should go to that link
– manish thakur
Nov 21 '18 at 12:11
I updated the snippet, check it you will get how to add link(url) to sub menus
– Aquib Iqbal
Nov 21 '18 at 12:23
I updated the snippet, check it you will get how to add link(url) to sub menus
– Aquib Iqbal
Nov 21 '18 at 12:23
wait ill test it
– manish thakur
Nov 21 '18 at 12:29
wait ill test it
– manish thakur
Nov 21 '18 at 12:29
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410013%2fhow-to-create-bootstrap4-navbar-with-menus-and-sub-menus-with-json-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@adiga i have tried this " for (var i in data) { $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>'); var div = '<div role="tabpanel" class="tab-pane" id="' + i + '">'; for (var j = 0; j < data[i].length; j++) { var obj = data[i][j]; div += '<div id="' + obj.id + '">' + obj.type + '</div>'; } $('.tab-content').append(div); }" i ahve found this one from google only but its only working for ul tag and not for nav
– dheeraj kumar
Nov 21 '18 at 10:36
Please see this example: jsfiddle.net/saslam/vepeLmm0
– Farhad Mortezapour
Nov 21 '18 at 10:37
@FarhadMortezapour its not talkin bootstrap cl;asses and not even form the toggle button when used for small device
– manish thakur
Nov 21 '18 at 11:17
@FarhadMortezapour that was not helpfull at all ;(
– manish thakur
Nov 21 '18 at 11:39