I have 3 questions about the same thing that I need to solve:
1
How can I make a loop inside the another loop to get price_history
from .json
file and populate the modal with the 7 results following the template inside the priceHistoryModal
variable?
I've had tried some ways, but all returns me undefined
.
2
Only the first button "Price History" on'click
' shows all modal <div>
at the same time. How can I open his parent modal only (inside the same <article>
)?
3
Why the last modal don't close? As the same with button, just the first one calls the modal.
This is the .json
file:
{
"hotels": [
{
"name": "Super 8 Speedway/University",
"description": "Just off Interstate 85, this hotel features free Wi-Fi and a daily continental breakfast with hot waffles. The Charlotte Motor Speedway is a 15 minute drive from the hotel.",
"image": "http://www.raphaelfabeni.com.br/rv/test-resources/hotels/super-8.jpg",
"rate": 3,
"price": 180,
"price_history": [
{ "month": "Jan", "value": 146 },
{ "month": "Feb", "value": 182 },
{ "month": "Mar", "value": 130 },
{ "month": "Apr", "value": 119 },
{ "month": "May", "value": 171 },
{ "month": "Jun", "value": 154 },
{ "month": "Jul", "value": 163 }
]
},
{
"name": "Omni Charlotte Hotel",
"description": "The Omni Charlotte Hotel envelopes you in ultimate comfort with a signature touch of genuine North Carolina hospitality.",
"image": "http://www.raphaelfabeni.com.br/rv/test-resources/hotels/omni-charlotte.jpg",
"rate": 4,
"price": 280.50,
"price_history": [
{ "month": "Jan", "value": 302 },
{ "month": "Feb", "value": 219 },
{ "month": "Mar", "value": 300 },
{ "month": "Apr", "value": 229 },
{ "month": "May", "value": 272 },
{ "month": "Jun", "value": 249 },
{ "month": "Jul", "value": 239 }
]
}
And the .js
file with the my issue section:
$(".searchHotels").on('click', function() {
//Get JSON Data
$.getJSON('http://www.raphaelfabeni.com.br/rv/hotels.json', function(data) {
//Start loop
$.each(data.hotels, function(k, v) {
//Stars rating template
var span = '<span id="hotelStars" class="fa fa-star starRating-' + this['rate'] + '"></span>';
var priceHistoryModal = '<div>' +
'<p>Price History</p>' +
//Question #1
//Need to show all 7 months
'<p>Month:' + this.price_history[0]['month'] + '</p>' +
//Need to show all 7 month prices
'<p>Price:' + this.price_history[0]['value'] + '</p>' +
'<p>Click to close</p>' +
'</div>';
// Hotel Results Template
var html = '<article id="hotelInfo" class="hotelInfoWrapper" data-rating="' + this['rate'] + '" data-price="' + this['price'] + '" style="position: relative;">' +
'<div class="modalCtn" style="width: 450px; height: 300px; background: #79BD1A; position: absolute; display: none; margin-left: 20%;">' +
//Question #2
//Here I need do another loop and get the price history: month and value of each hotel result (data.hotels) from the loop above.
//And append the priceHistoryModal as template
priceHistoryModal + //its correct?
'</div>' +
'<div class="imgCtn"><img src="' + this['image'] + '" alt=""></div>' +
'<div class="hotelInfo">' +
span +
'<h1>' + this['name'] + '</h1>' +
'<p>' + this['description'] + '</p>' +
'<button class="bookNow">Book now</button>' +
'<button id="showPriceHistory" class="priceHistory">Price history</button>' +
'</div>' +
'<div class="hotelPrice">' +
'<small>Total</small>' +
'<h2 class="price">$' + this['price'] + '</h2>' +
'</div>' +
'</article>';
//Question #3
//This button calls the modal with price history container
//But just the first button works and call all modals at the same time
//On click need to call only the modal inside the same button container
$("#showPriceHistory").on('click', function() {
$(".modalCtn").fadeIn();
});
//Just for close modal
$(".modalCtn").on('click', function() {
$(this).fadeOut();
});
//Append the html template variable to the hotel list
$(html).hide().appendTo("#hotelList").fadeIn(400);
}); //End Loop
}); //End Ajax
}); //End Button Search Hotels Event
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hotelList"></div>
Thank you!
'+this['month']+'
'); console.log(k); });` And this load the data, but load all months in the same `