-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
134 lines (116 loc) · 5.39 KB
/
App.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// app.js
//ระบบซื้อของ smartphone มือ 2
document.addEventListener('DOMContentLoaded', () => {
const smartphoneList = document.getElementById('smartphone-list');
const addSmartphoneForm = document.getElementById('add-smartphone-form');
const modelInput = document.getElementById('model');
const priceInput = document.getElementById('price');
const descriptionInput = document.getElementById('description');
const buyModal = document.getElementById('buyModal');
const buyModalText = document.getElementById('buyModalText');
const cancelBuyButton = document.getElementById('cancelBuyButton');
const confirmBuyButton = document.getElementById('confirmBuyButton');
let currentBuyIndex = null;
const smartphones = [
{ model: 'xx1', price: 600, description: 'A great phone with a powerful chip and great camera.' },
{ model: 'xxx2', price: 500, description: 'A high-end smartphone with an amazing display.' },
{ model: 'xxxx3', price: 450, description: 'A solid phone with a fantastic camera and clean software.' }
];
const renderSmartphones = () => {
smartphoneList.innerHTML = '';
smartphones.forEach((phone, index) => {
const phoneElement = document.createElement('div');
phoneElement.classList.add('bg-white', 'p-4', 'rounded', 'shadow-md');
phoneElement.innerHTML = `
<h3 class="text-xl font-bold">${phone.model}</h3>
<p class="text-gray-700">$${phone.price}</p>
<p class="text-gray-600">${phone.description}</p>
<div class="mt-2">
<button class="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-2 rounded mr-2" onclick="buySmartphone(${index})">Buy</button>
<button class="bg-red-500 hover:bg-red-700 text-white font-bold py-1 px-2 rounded" onclick="removeSmartphone(${index})">Remove</button>
</div>
`;
smartphoneList.appendChild(phoneElement);
});
};
const addSmartphone = (event) => {
event.preventDefault();
const newSmartphone = {
model: modelInput.value,
price: parseFloat(priceInput.value),
description: descriptionInput.value
};
smartphones.push(newSmartphone);
renderSmartphones();
addSmartphoneForm.reset();
};
window.removeSmartphone = (index) => {
smartphones.splice(index, 1);
renderSmartphones();
};
window.buySmartphone = (index) => {
currentBuyIndex = index;
const phone = smartphones[index];
buyModalText.textContent = `Are you sure you want to buy the ${phone.model} for $${phone.price}?`;
buyModal.classList.remove('hidden');
};
cancelBuyButton.addEventListener('click', () => {
buyModal.classList.add('hidden');
currentBuyIndex = null;
});
confirmBuyButton.addEventListener('click', () => {
if (currentBuyIndex !== null) {
smartphones.splice(currentBuyIndex, 1);
renderSmartphones();
buyModal.classList.add('hidden');
currentBuyIndex = null;
alert('Purchase successful!');
}
});
addSmartphoneForm.addEventListener('submit', addSmartphone);
renderSmartphones();
});
// ระบบขายของ smartphone มือ2
document.addEventListener('DOMContentLoaded', () => {
const smartphoneList = document.getElementById('smartphone-list');
const addSmartphoneForm = document.getElementById('add-smartphone-form');
const modelInput = document.getElementById('model');
const priceInput = document.getElementById('price');
const descriptionInput = document.getElementById('description');
const smartphones = [
{ model: 'xx1', price: 600, description: 'A great phone with a powerful chip and great camera.' },
{ model: 'xxx2', price: 500, description: 'A high-end smartphone with an amazing display.' },
{ model: 'xxxx3', price: 450, description: 'A solid phone with a fantastic camera and clean software.' }
];
const renderSmartphones = () => {
smartphoneList.innerHTML = '';
smartphones.forEach((phone, index) => {
const phoneElement = document.createElement('div');
phoneElement.classList.add('bg-white', 'p-4', 'rounded', 'shadow-md');
phoneElement.innerHTML = `
<h3 class="text-xl font-bold">${phone.model}</h3>
<p class="text-gray-700">${phone.price} Bath</p>
<p class="text-gray-600">${phone.description}</p>
<button class="bg-red-500 hover:bg-red-700 text-white font-bold py-1 px-2 rounded mt-2" onclick="removeSmartphone(${index})">Remove</button>
`;
smartphoneList.appendChild(phoneElement);
});
};
const addSmartphone = (event) => {
event.preventDefault();
const newSmartphone = {
model: modelInput.value,
price: parseFloat(priceInput.value),
description: descriptionInput.value
};
smartphones.push(newSmartphone);
renderSmartphones();
addSmartphoneForm.reset();
};
window.removeSmartphone = (index) => {
smartphones.splice(index, 1);
renderSmartphones();
};
addSmartphoneForm.addEventListener('submit', addSmartphone);
renderSmartphones();
});