Answer the question
In order to leave comments, you need to log in
How to turn ajax response into layout?
I get a response from Ajax in the form of an array.
Here I'm trying to turn it into a layout in a cycle.
success: function (resik) {
console.log(resik);
console.log(resik.length);
if (resik.length!==0)
{
for (var i = 0; i < resik.length; i++) {
var resres = resik[i].time;
$(".panel").append('<div class="panel-heading">'+
'<h4 class="panel-title">'+
+ '<a data-toggle="collapse" data-parent="#accordion-alt3" href="#collapse1-alt3">'+
+ '<i class="fa fa-stack-exchange"></i>'+ v +
+'</a>'
+'</h4>'
+ '</div>');
}
}
else
{
$(".panel").append('<div class="panel-heading">'+
'<h4 class="panel-title">'+
+ '<a data-toggle="collapse" data-parent="#accordion-alt3" href="#collapse1-alt3">'+
+ '<i class="fa fa-stack-exchange"></i>'+ 'На данную дату талонов нет'+
+'</a>'
+'</h4>'
+ '</div>');
}
},
error:function () {
alert('Error');
}
Answer the question
In order to leave comments, you need to log in
try this option
success: function (resik) {
var time_list = [];
var time_html = [];
if (Array.isArray(resik)){
resik.forEach(function(res){
time_list.push(res.time);
});
}
if (!time_list.length)
time_list.push('На данную дату талонов нет');
time_list.forEach(function (time) {
time_html.push('<div class="panel-heading"><h4 class="panel-title">' +
'<a data-toggle="collapse" data-parent="#accordion-alt3" href="#collapse1-alt3">' +
'<i class="fa fa-stack-exchange"></i>${time}</a></h4></div>');
});
$(".panel").html(time_html.join('\n'));
},
error: function () {
alert('Error');
}
+ '<a data-toggle="collapse" data-parent="#accordion-alt3" href="#collapse1-alt3">'+
+ '<i class="fa fa-stack-exchange"></i>'+ 'На данную дату талонов нет'+
+'</a>'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question