Looping JSON into DIV - asp.net-mvc

I have the JSON from my controller as below:
{"ja":
[
{
"ID":0,
"Name":"Son",
"DepartmentID":0,
"isNewProduct":false,
"PictureName":null,
"isInstock":false
}
,
{
"ID":0,
"Name":"AM4-B2",
"DepartmentID":0,
"isNewProduct":false,
"PictureName":null,
"isInstock":false
}
,
{
"ID":0,
"Name":"AM4-B2",
"DepartmentID":0,
"isNewProduct":false,
"PictureName":null,
"isInstock":false
}
]
}
This is my menu that I'm going to click on it :
<ul id="navmenu-v">
<li id="select_list" class="level1">
<a id="56" class="selectedcategory" href="javascript:void(0);">Accessories</a>
</li>
<li id="select_list" class="level1">
<a id="69" class="selectedcategory" href="javascript:void(0);">Audio Players</a>
</li>
</ul>
And these are the div that I want to put each json in in :
<div id="container">
<div id="id"></div>
<div id="name"></div>
<div id="departmentID"></div>
<div id="isNew"></div>
<div id="pictureName"></div>
<div id="isStock"></div>
</div>
then I want to loop all the JSON into different div beside the in my view when I click on my Menu.
Could anyone tell me how can I do that using $.getJSON() and $.each()? Thanks so much.

first ull have to bind the menu anchor elements to the click events like this
$("#56").click(function(){
$.getJSON("baseUrl/Accesories",function(data){
$.each(data,function(index,value){
$('div#id').html($('div#id').html()+value.ID);
//simillarily you can do for others too
});
});
});

Related

jQuery Ui-Autocomplete and popover in input tag

I want to use autocomplete and popover in one input tag.
But autocomplete can't display , I see google chrome developer tools, autocomplete is work, just can't display . I don't know how to do .
HTML code
<span class="form-labelTitle img-searchForm02">place</span>
<input class="auto-input" placeholder="city" data-toggle="popover" data-placement="bottom">
<div class="leave-btn"></div>
<div class="autocomplete-div auto1"></div>
<div id="popover-content" class="hide">
<div role="tabpanel">
<ul class="nav nav-tabs arrival-tabs" role="tablist" id="myTab">
<li class="active" role="presentation" ><a href='#JPN' role='tab' data-toggle='tab'>japan</a></li>
<li role="presentation" ><a href='#TPE' role='tab' data-toggle='tab'>taiwan</a></li>
</ul>
<div class="tab-content">
<div class='tab-pane active city' id='JPN'>
<span>Tokyo</span>
<span>okinawa</span>
</div>
<div class='tab-pane' id='TPE'>
<span>taipei</span>
</div>
JS code
$(function(){
$('.auto-input').popover({
'html': true,
'content': function () {
return $('#popover-content').html();
}
});
$('.auto-input').autocomplete({
open:function(event, ui){
$(this).popover('hide');
},
source: [
"tokyo",
"okinawa",
"taipei
],
appendTo:($('.auto1')),
});
});

Nested ListView In Jquery Mobile with mvc

I create nested list view in Asp.net mvc with jQuery mobile, it has 2 sub view, when i navigate to first subview1 i can return back to main view, but when i navigate from subview1 to subview2 i can not back to subview1, since subview1 is somehow null.
in my layout page i put this script
<script>
$(document).on('mobileinit', function () {
$.mobile.page.prototype.options.addBackBtn = true;
$.mobile.page.prototype.options.backBtnText = "Back";
$.mobile.page.prototype.options.backBtnTheme = "a";
$.mobile.defaultPageTransition = "slide";
});
</script>
the main page is :
<script>
$(document).on('click', '[data-rel=back]', function (e) {
e.preventDefault();
var activepage = $.mobile.activePage.attr("id");
if (activepage == "schedule")
$.mobile.changePage($('#flight'), { transition: "slide", reverse: false });
if (activepage == "dayFlight") {
$.mobile.changePage($('#schedule'), { transition: "slide", reverse: false });
}
});
<div data-role="page" id="flight" data-add-back-btn="true">
<div data-role="content">
<ul id="flightListView" data-role="listview" data-inset="false" data-filter="true">
#foreach (var item in Model)
{
<li>
<a href="#Url.Action("Schedules", new { from = item.DepartureFrom, to = item.Destination })" data-direction="reverse" data-transition="slide">
<span class="ui-li-count ui-btn-up-c ui-btn-corner-all">#item.NumberOfFlights</span>
</a>
</li>
}
</ul>
</div>
the subview1 is:
<div data-role="page" data-add-back-btn="true" id="schedule">
<div data-role="content">
<ul id="scheduleListView" data-role="listview" data-inset="false" data-filter="false">
#foreach (var item in Model)
{
<li>
<a href="#Url.Action("DayFlights", new { from = from, to = to, date = item.Date })" data-direction="reverse" data-transition="slide">
<span class="lg">#Html.DisplayFor(modelItem => item.Day)</span>
<span class="lg number laligned">#Html.DisplayFor(modelItem => item.Date)</span>
<span class="ui-li-count ui-btn-up-c ui-btn-corner-all">#item.Nof</span>
</a>
</li>
}
</ul>
</div>
the subview2 is :
<div data-role="page" data-add-back-btn="true" id="dayFlight">
<div data-role="content">
<ul id="dayListView" data-role="listview" data-inset="true" data-filter="false">
#foreach (var item in Model)
{
<li>
<a href="#item.AgencyUrl" rel="external" data-ajax="false">
<span class="ui-li-count ui-btn-up-c ui-btn-corner-all">#item.Cap</span>
</a>
</li>
}
</ul>
</div>
something that i figure out, it is when i navigate to subview1, the mainview and the subview1 both of them are in DOM but then when i navigate to subview2, subview1 will replaced by subview2, so it means when i am at subview2 the subview1 does not exist in DOM.
in all cases $.mobile.activePage.prev('.ui-page'); return null, that is the reason why i decided to use if clause to determine to which page i should navigate back to, however when i want to navigate back from subview2 to subview1 it does not work.
how can i implement this senario?
thanks in advance

Can't click on dropdown on bootstrap navbar after something else on navbar has been clicked

I have a normal bootstrap navbar with some drop downs on it. When I click on a link in a drop down a modal box appears on screen, when I exit the modal box and then try to click on the drop down again on the navbar it won't work, an orange square border appears around the dropdown as if it can't get focus again or something. Any ideas why this could be happening?
<div class="navbar navbar-inverse navbar-fixed-top ">
<div class="navbar-inner" id="background">
<a class="brand" href="#Url.Action("RssIndex", "Home")">
<img src="~/Images/iconnewzy.png" alt="Newzy" ></a>
<ul class="nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="icon-user"></i>
<label style="display: inline; cursor: pointer;" id="friendsId">Friends</label><b class="caret"></b></a>
<ul class="dropdown-menu">
Add Friend
View Friends
</ul>
</li>
<b style="vertical-align: bottom !important; color: white">Hello, #User.Identity.Name!</b>
#Html.ActionLink("Log off", "LogOff", "Account", routeValues: null, htmlAttributes: new { #class = "btn btn-mini btn-primary" })
</ul>
</div>
</div>
I had the same problem. I've found a workaround in this article. Basically, you have to add this code to your page:
$(function() {
// Setup drop down menu
$('.dropdown-toggle').dropdown();
// Fix input element click problem
$('.dropdown input, .dropdown label').click(function(e) {
e.stopPropagation();
});
});

jQuery toggleClass and slideToggle

I'm toggling a class that hides and unhides a div within a list item, but I'm having problems applying a slideToggle to it.
Here is the HTML and CSS;
<style>
.collapsed .widget-content {
display:none !important;
}
</style>
<ul id="column1" class="column">
<li class="widget color-green" id="intro">
<div class="widget-head">
<a class="collapse" href"#">collapse</a>
<h3>The Title</h3>
</div>
<div class="widget-content">
<p>content here</p>
</div>
</li>
</ul>
Here is the jQuery to toggle the class of the li on click on the link with the class "collapse"
$(this).parents(settings.widgetSelector).toggleClass('collapsed');
How do I apply the slideToggle so that it only slides away the "widget-content" div whilst toggling the "collapsed" class on the li at the same time?
Thanks in advance for your advice.
you could try a callback when the toggle is complete:
$(this).parents(settings.widgetSelector).slideToggle('slow', function() {
$('#intro').toggleClass('collapsed');
});
something like that?

Asp.Net MVC load a partial view using ajax from a JQuery UI tab

I have several action methods on my controller that return a partial view (ascx), and I want these partial views to be rendered when clicking on the different JQuery UI tabs.
The tabs are defined like this:
<div id="tabs">
<li>First tab</li>
<li><%= Html.ActionLink("General", "General", new {id=Model.Id}) %></li>
<li><%= Html.ActionLink("Details", "Details", new {id=Model.Id}) %></li>
<div id="FirstTab">
...
</div>
</div>
This renders the tabs fine, the first tab (with static content) is displayed correctly, but when I click the other tabs, nothing happens.
If instead of returning a partial view from the action methods I just return plain content, it renders the content just fine and the tabs work perfectly.
Any idea of what am I doing wrong?
Thanks
Hey, I've done the same thing recently. I simplified in order to be more clear:
The html:
<div class="tabs">
<ul>
<li>
<a onclick="TabItemClicked(this,<%=Url.Action("General", new {id=Model.Id}) %>))" href="#fragment1">
<span>General</span>
</a>
</li>
<li>
<a onclick="TabItemClicked(this,<%= Html.ActionLink("Details", new {id=Model.Id}) %>))" href="#fragment2">
<span>Details</span>
</a>
</li>
</ul>
<div id="fragment1"></div>
<div id="fragment2"></div>
</div>
and the JQuery code:
function TabItemClicked(a, action) {
var container = $(a).parents('div.tabs');
var resultDiv = $($(a).attr('href'), container);
$.ajax({
type: "POST",
url: action,
data: {},
success: function(response) {
resultDiv.html('');
resultDiv.html(response);
}
});
}

Resources