$(function () {
loadItems();
});
function loadItems() {
$.getJSON('database/products.json', function (data) {
function loadCakes(num) {
let out = '';
if (!num) {
for (let key in data) {
out += '<a data-num="' + data[key].id + '" data-category="' + data[key]['category'] + '" class="products__card card">';
out += '<div class="card__image image">';
out += '<img src="' + data[key].image + '" alt="' + data[key]['image'] + '">';
out += '</div>';
out += '<h3 class="card__title title">' + data[key]['name'] + '</h3>';
out += '<p class="card__desc">' + data[key]['description'] + '</p>';
out += '<div class="card__bottom">';
out += '<p class="card__price">' + data[key].price + ' р.</p>';
out += '<button class="card__buy">';
out += '<img src="images/icons/shopping-cart-add.svg" alt="shopping-cart-add">';
out += '</button>';
out += '</div>';
out += '</a>';
}
} else {
for (let i = 1; i <= num; i++) {
out += '<a data-num="' + data[i].id + '" data-category="' + data[i]['category'] + '" class="products__card card">';
out += '<div class="card__image image">';
out += '<img src="' + data[i].image + '" alt="' + data[i]['image'] + '">';
out += '</div>';
out += '<h3 class="card__title title">' + data[i]['name'] + '</h3>';
out += '<p class="card__desc">' + data[i]['description'] + '</p>';
out += '<div class="card__bottom">';
out += '<p class="card__price">' + data[i].price + ' р.</p>';
out += '<button class="card__buy">';
out += '<img src="images/icons/shopping-cart-add.svg" alt="shopping-cart-add">';
out += '</button>';
out += '</div>';
out += '</a>';
}
}
return out;
}
$('#cakes-cards').html(loadCakes());
$('#index-cards').html(loadCakes(6));
})
}
function render(arr) {
return `<a data-num="${arr.id}" data-category="${arr.category}" class="products__card card">
<div class="card__image image">
<img src="${arr.image}" alt="${arr.image}">
</div>
<h3 class="card__title title">${arr.name}</h3>
<p class="card__desc">${arr.description}</p>
<div class="card__bottom">
<p class="card__price">${arr.price} р.</p>
<button class="card__buy">
<img src="images/icons/shopping-cart-add.svg" alt="shopping-cart-add">
</button>
</div>
</a>`;
}
function loadCakes(data, count = null) {
return data
.slice(0, count ?? data.length)
.reduce((carry, item) => carry + render(item), '');
}
function loadItems() {
$.getJSON('database/products.json', function (data) {
$('#cakes-cards').html(loadCakes(data));
$('#index-cards').html(loadCakes(data, 6));
});
}
$(function() {
loadItems();
});
function loadItems() {
$.getJSON('database/products.json', function(data) {
function loadCakes(num) {
let out = '';
for (let i = 0; i < (num || data.length); i++) {
out += '<a data-num="' + data[i].id + '" data-category="' + data[i]['category'] + '" class="products__card card">';
out += '<div class="card__image image">';
out += '<img src="' + data[i].image + '" alt="' + data[i]['image'] + '">';
out += '</div>';
out += '<h3 class="card__title title">' + data[i]['name'] + '</h3>';
out += '<p class="card__desc">' + data[i]['description'] + '</p>';
out += '<div class="card__bottom">';
out += '<p class="card__price">' + data[i].price + ' р.</p>';
out += '<button class="card__buy">';
out += '<img src="images/icons/shopping-cart-add.svg" alt="shopping-cart-add">';
out += '</button>';
out += '</div>';
out += '</a>';
}
return out;
}
$('#cakes-cards').html(loadCakes());
$('#index-cards').html(loadCakes(6));
})
}