-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.js
60 lines (47 loc) · 1.8 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
async function loadMenuItemsFromCSV(csvFile, containerId) {
try {
const response = await fetch(csvFile);
const data = await response.text();
displayMenuItems(data, containerId);
} catch (error) {
console.error("Ошибка при загрузке CSV:", error);
}
}
function displayMenuItems(data, containerId) {
const items = parseCSV(data);
const container = document.getElementById(containerId);
container.innerHTML = '';
items.forEach(item => {
const { name, description, price } = item;
const dishCard = document.createElement('div');
dishCard.classList.add('dish-card');
const dishInfo = document.createElement('div');
dishInfo.classList.add('dish-info');
const dishName = document.createElement('h2');
dishName.classList.add('dish-name');
dishName.textContent = name;
const dishDescription = document.createElement('p');
dishDescription.classList.add('dish-description');
dishDescription.textContent = description;
dishInfo.appendChild(dishName);
dishInfo.appendChild(dishDescription);
const dishPrice = document.createElement('div');
dishPrice.classList.add('dish-price');
dishPrice.textContent = price;
dishCard.appendChild(dishInfo);
dishCard.appendChild(dishPrice);
container.appendChild(dishCard);
});
}
function parseCSV(data) {
const rows = data.trim().split('\n');
const items = [];
for (let i = 1; i < rows.length; i++) {
const row = rows[i].match(/(".*?"|[^",]+)(?=\s*,|\s*$)/g);
if (row) {
const [name, description, price] = row.map(cell => cell.replace(/^"|"$/g, ''));
items.push({ name, description, price });
}
}
return items;
}