0
Hello guys I’m having a problem and I wanted a help.
How can I make one for
inside html +=
ajax?
JS:
$('.requerAjax').click(function(e) {
e.preventDefault();
var mes = $(this).attr('rel');
$.ajax({
type: "POST",
url: "/admin/galeria/fotos-ajax",
data: "mes="+ mes,
datatype: 'json',
beforeSend: function(){
},
success: function(result){
var i = 0;
var result = result[i];
// console.log(result);
var html = "";
html += "<div id="+ result.mes + result.ano +" class='modal fade' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>";
html += "<div class='modal-dialog'>";
html += "<div class='modal-content'>";
html += "<div class='modal-header'>";
html += "<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>";
html += "<h4 class='modal-title'>"+ result.mes +"/"+ result.ano +"</h4>";
html += "</div>";
html += "<div class='modal-body'>";
html += "<div class='gallery'>";
// for ( $i < result.length; $i++) {
html += "<img src="+ result.imagem +" alt=''>";
// }
html += "</div>";
html += "</div>";
html += "<div class='modal-footer'>";
html += "<button type='button' class='btn btn-default' data-dismiss='modal' aria-hidden='true'>Fechar</button>";
html += "</div>";
html += "</div>";
html += "</div>";
html += "</div>"
$( ".areaModal" ).html(html);
}
});
I have a modal that inside it will come several photos. I want to put For into the modal, in here:
html += "<div class='gallery'>";
// for ( $i < result.length; $i++) {
html += "<img src="+ result.imagem +" alt=''>";
// }
html += "</div>";
Please add the JSON you are receiving to the question (you can do it
console.log(JSON.stringify(result));
at the very beginning of this function).– Sergio
He returns to me
{"mes":"Dezembro","ano":"2014","imagem":"/assets/galeria/fotos/2014_Dezembro_1_1_1.jpg"}
– MichaelFernandes
Okay, but is it just an object? or an array of objects? Are you doing this
console.log
before or aftervar result = result[i];
? And why do you have it like this? you are limiting the results to the first of the array... (I am asking questions to better understand the problem and be able to help better)– Sergio
It’s just an object, this console.log
console.log(JSON.stringify(result));
is just below thevar result = result[i];
. Thisvar result = result[i];
so I can get the other results that’s coming in likehtml += "<h4 class='modal-title'>"+ result.mes +"/"+ result.ano +"</h4>";
.– MichaelFernandes
But then the
result
original (function argument) is an array. Aren’t the other images there? because in the JSON you placed the keyimagem
there’s only onesrc
and you want to loop what images?– Sergio
If I take the
var i = 0; var result = result[i];
and leave theconsole.log(JSON.stringify(result));
he return me the 3 images[{"mes":"Dezembro","ano":"2014","imagem":"/assets/galeria/fotos/2014_Dezembro_1_1_1.jpg"},{"mes":"Dezembro","ano":"2014","imagem":"/assets/galeria/fotos/2014_Dezembro_1_1_2.jpg"},{"mes":"Dezembro","ano":"2014","imagem":"/assets/galeria/fotos/2014_Dezembro_1_1_3.jpg"},{"mes":"Dezembro","ano":"2014","imagem":"/assets/galeria/fotos/2014_Dezembro_1_1_4.jpg"}]
– MichaelFernandes
Okay and those are the images you want to have on
for
and in modal?– Sergio
Yeah, but now he’s not riding the modal, I guess because I’m calling in 2 places
+ result.mes +"/"+ result.ano +
if I put theconsole.log(JSON.stringify(result.imagem));
he take me backundefined
, but if I go in Network in Preview he return me the 3 images.– MichaelFernandes
In HTML with class
'.requerAjax'
is the month information right? has the year information somewhere also in HTML?– Sergio
Look, I gotta go. I think what you need is this: http://jsfiddle.net/h5z3d86d/ - I would like to know more data to give you the best answer, but maybe tomorrow. Notice that I removed that
var i = 0; var result = result[i];
and gave the year as 2014. Ideally it would also be in HTML. In next(s) questions here puts more data that you will get better answers. Good year!– Sergio
Yes it has to look, this is the image: http://tinypic.com/r/2zhfiie/8
– MichaelFernandes
A
underscore.js
in that case would suit me very well!– Wallace Maxters