Reputation: 11652
I want to build sub menu for menu item from json data.
<div class="subnav-fixed" id="menuContrainer" runat="server">
<ul class="nav nav-pills">
<li id="Li0"><a href="Default.aspx">Home </li>
<li id="Li1"><a href="InitiativeGrid.aspx">Initiative</a></li>
<li id="Li2"><a href="Reports.aspx">Reports</a></li>
<li id="Li3"><a href="EditInitiative.aspx">Edit Initiatives</a></li>
</ul>
</div>
data =
"{"d":[
{"__type":"Tableau_Reports:#CostReductionData",
"ClientIdx":1,
"GroupName":"HR",
"ReportGroup":"1",
"ReportHeight":"800",
"ReportName":"Baseline Vs Active Employees",
"ReportOrder":"0",
"ReportUrl":"https://company.com/t/sga/views/HRReports/BaselineandActiveEmployees"
},
{"__type":"Tableau_Reports:#CostReductionData",
"ClientIdx":1,
"GroupName":"HR",
"ReportGroup":"1",
"ReportHeight":"800",
"ReportName":"Level vs Direct Reports",
"ReportOrder":"0",
"ReportUrl":"https://company.com/t/sga/views/HRReports/LevelvsDirectReports"
},
{"__type":"Tableau_Reports:#Alixpartners.SGACostReductionData",
"ClientIdx":1,
"GroupName":"Finance",
"ReportGroup":"2",
"ReportHeight":"800",
"ReportName":"Spans and Layers",
"ReportOrder":"0",
"ReportUrl":"https://company.com/t/sga/views/HRReports/SpansandControl"
}]
}"
I want to display sub menu for Report menu Item like this
Home Initiative Reports Edit Initiative
|
HR- Baseline Vs Active Employees
- Level vs Direct Reports
|
Finance - Spans and Layers
How can we do this with Jquery?
Upvotes: 1
Views: 15843
Reputation: 1527
try this logic
var html='<ul>';
$.each(data.d, function(i, item) {
//alert(item.GroupName);
html+='<li><a>'+item.GroupName+'</a></li>'
});
html+='</ul>';
Upvotes: 0
Reputation: 22570
I'm not sure how you're getting your data, but using jquery to add HTML is phenomenally easy. Base example, you are using $.ajax() to get your data. With your given HTML, and JSON data return, you might do something like:
$(function() {
$.ajax({
url: "http://www.yourDomain.com/yourController/yourMethod",
dataType: "json",
type: "get",
beforeSend: function(xhr, settings) {
$("#Li2").find("ul").remove();
},
success: function(data, status, xhr) {
if (data["d"]) {
if (data["d"].length) {
var items = data["d"],
ul = $("<ul />").appendTo($("#Li2"));
for (x in items) {
var li = $("<li />").appendTo(ul);
li.append($("<a />", { href: items[x].ReportUrl, text: items[x].ReportName }));
}
}
}
}
})
})
OR if the JSON is a variable in your JS then you would just use $.each() with same type setup:
$(function() {
var $ul = $("<ul />").appendTo($("#Li2"));
$.each(data.d, function(index, item) {
var li = $("<li />").appendTo($ul);
li.append($("<a />", { href: item.ReportUrl, text: item.ReportName }));
})
})
And just for thoroughness, a combo of the 2:
$(function() {
$.ajax({
url: "http://www.yourDomain.com/yourController/yourMethod",
dataType: "json",
type: "get",
beforeSend: function(xhr, settings) {
$("#Li2").find("ul").remove();
},
success: function(data, status, xhr) {
if (data["d"]) {
if (data["d"].length) {
var ul = $("<ul />").appendTo($("#Li2"));
$.each(data.d, function(index, item) {
var li = $("<li />").appendTo(ul);
li.append($("<a />", { href: items[x].ReportUrl, text: items[x].ReportName }));
});
}
}
}
})
})
Further Reading:
Upvotes: 4