-
Notifications
You must be signed in to change notification settings - Fork 36
/
menu.js
68 lines (60 loc) · 2.32 KB
/
menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
document.addEventListener('DOMContentLoaded', () => {
fetchMenuData();
});
async function fetchMenuData() {
try {
const response = await fetch('menu.json');
const data = await response.json();
renderMenu(data.menuItems);
} catch (error) {
console.error('Error loading menu data:', error);
}
}
function renderMenu(menuItems) {
const menuGrid = document.getElementById('menuGrid');
const categoryButtons = document.getElementById('categoryButtons');
// Store unique categories
const categories = [...new Set(menuItems.map(item => item.category))];
// Create category buttons
categories.forEach(category => {
const button = document.createElement('button');
button.className = 'category-button';
button.textContent = category;
button.onclick = () => toggleCategoryItems(category, menuItems);
categoryButtons.appendChild(button);
});
// Render all menu items but hide them initially
menuGrid.innerHTML = menuItems.map(item => {
return `<div class="menu-item hidden" data-category="${item.category}">
<div class="menu-item-content">
<div class="menu-item-front">
<img src="${item.image}" alt="${item.name}" class="menu-item-image">
<h2 class="menu-item-title">${item.name}</h2>
</div>
<div class="menu-item-back">
<h2>Additional Info</h2> <!-- You can add additional info here -->
<p class="menu-item-description">${item.description}</p>
<button class="menu-item-price">${item.price}</button>
<p>This is some extra content on the back!</p>
</div>
</div>
</div>
`;
}).join('');
// Show items for the first category by default
if (categories.length > 0) {
toggleCategoryItems(categories[0], menuItems);
}
}
function toggleCategoryItems(selectedCategory, menuItems) {
const menuGrid = document.getElementById('menuGrid');
const items = menuGrid.querySelectorAll('.menu-item');
items.forEach(item => {
// Check if the item's category matches the selected category
if (item.dataset.category === selectedCategory) {
item.classList.remove('hidden'); // Show the item
} else {
item.classList.add('hidden'); // Hide the item
}
});
}