A simple web application was developed by me to retrieve recipe data from an API. This data is then displayed by inserting it into an HTML template specified in the JavaScript file. The layout is managed using a float grid in CSS.
Here is the code snippet responsible for rendering the result and inserting it into the template:
function displayRecipeSearchData(data) {
var results = ' ';
if (data.hits.length) {
data.hits.forEach(function(item) {
results += template.item(item);
});
}
else {
results += '<p> No results </p>';
}
$('#js-search-results').html(results);
}
The HTML template used to display responses:
const template = {
item: function(item) {
return '<div class ="col-4">' +
'<div class ="result">' +
'<div class="recipelabel">' +
'<div class="reclist">' + item.recipe.ingredientLines + '</div><!-- end reclist -->' +
'<p class="label">' + item.recipe.label + '</p>' +
'<div class="thumbnail">' +
'<a href="'+ httpsTransform(item.recipe.url) + '" target="_blank">' +
'<img src="' + item.recipe.image + '"alt="' + item.recipe.label + '">' +
'</a>' +
'<div class="recipesource">' +
'<p class="source">' + item.recipe.source + '</p>' +
'</div><!-- end recipesource -->' +
'</div><!-- end thumbnail -->' +
'</div><!-- end recipelabel -->' +
'</div><!-- end result -->' +
'</div><!-- end col-4 -->';
}
};
I am attempting to modify the logic in the displayRecipeSearchData function so that a <div></div>
encompasses every group of three results. This will ensure that the rows/columns function correctly in the flex grid. I have experimented with various approaches, but have not achieved the correct syntax/logic yet. Would nesting an if statement within the existing statement be an effective solution?
if(i % 3 === 0 ){ results. += '<div class="row">''</div>'}
Any advice or suggestions on this matter would be greatly appreciated.