-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
177 lines (129 loc) · 5.7 KB
/
script.js
File metadata and controls
177 lines (129 loc) · 5.7 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
let products = [
{ url: 'images/f1.jpg', title: 'Printed Yellow Cotton Men Half T Shirt', price: '29' },
{ url: 'images/f2.jpg', title: 'Printed Green Cotton Men Half T Shirt', price: '22' },
{ url: 'images/f3.jpg', title: 'Printed Red Cotton Men Half T Shirt', price: '16' },
{ url: 'images/f4.jpg', title: 'Printed White Cotton Men Half T Shirt', price: '34' },
{ url: 'images/f5.jpg', title: 'Printed Blue Cotton Men Half T Shirt', price: '25' },
{ url: 'images/f6.jpg', title: 'Printed Orange Cotton Women Full T Shirt', price: '38' },
{ url: 'images/f7.jpg', title: 'Printed Burlywood Cotton Men Full T Shirt', price: '44' },
{ url: 'images/f8.jpg', title: 'Darkmagenta Cotton Women Half T Shirt', price: '30' }
];
let productRow = document.querySelector('.products');
let productTable = document.querySelector('#productTable');
let tableBody = document.querySelector('#tableBody');
let titleInput = document.querySelector('#inputTitle');
let priceInput = document.querySelector('#inputPrice');
let inputFile = document.querySelector('#inputFile');
let mainBtn = document.querySelector('#mainBtn');
let submitBtn = document.querySelector('#submitBtn');
// let newArray = products.map(prod => {
// prod.totalPrice = prod.price -prod.discount;
// return prod;
// })
// let srNo = 1;
let path;
inputFile.addEventListener('change', function () {
path = URL.createObjectURL(inputFile.files[0]);
})
function addItems() {
for (let key in products) {
console.log(products);
tableBody.innerHTML += `
<tr id="tableRow" style="vertical-align: middle;">
<td>${products[key]}</td>
<td>${products[key].title}</td>
<td>$${products[key].price}</td>
<td>
<img style="width:100px;" class="img-fluid" title="${products[key].title}" src="${products[key].url}" alt="${products[key].title}">
</td>
<td>
<button type="button" class="btn bg-danger" id="delBtn">Delete</button>
<button type="button" class="btn bg-success" id="editBtn">Edit</button>
</td>
</tr>
`;
// srNo++;
}
console.log(products);
mainFunction(products);
let tableRow = document.querySelectorAll('#tableRow');
let editBtn = document.querySelectorAll('#editBtn');
let delBtn = document.querySelectorAll('#delBtn');
for (let i = 0; i < delBtn.length; i++) {
delBtn[i].addEventListener('click', function () {
tableRow[i].remove();
})
}
for (const key in products) {
let updateBtn = document.createElement('button');
editBtn[key].addEventListener('click', function () {
titleInput.value = products[key].title;
priceInput.value = products[key].price;
let update = mainBtn.appendChild(updateBtn);
updateBtn.classList = ('btn bg-success w-100')
updateBtn.id = 'updateBtn';
updateBtn.innerText = 'Update';
mainBtn.replaceWith(update);
})
updateBtn.addEventListener('click', function () {
mainBtn.replaceWith(submitBtn);
console.log(titleInput.value + " " + priceInput.value);
// mainFunction(products);
})
}
}
submitBtn.addEventListener('click', function () {
let newProduct = { url: path, title: titleInput.value, price: priceInput.value }
products.push(newProduct);
tableBody.innerHTML += `
<tbody style="border-style: none; background-color: transparent;">
<tr id="tableRow" style="vertical-align: middle;">
<td>${newProduct[key]}</td>
<td>${newProduct.title}</td>
<td>$${newProduct.price}</td>
<td>
<img style="width:100px;" class="img-fluid" title="${newProduct.title}" src="${newProduct.url}" alt="${newProduct.title}">
</td>
<td>
<button type="button" class="btn bg-danger" id="delBtn">Delete</button>
<button type="button" class="btn bg-success" id="editBtn">Edit</button>
</td>
</tr>
</tbody>
`;
console.log(products);
// addItems();
})
function mainFunction(item) {
let html = ``;
for (const key in item) {
html += `
<div class="col-xl-3 col-lg-4 col-md-6 col-12">
<div class="card main-card">
<div class="card-header px-md-4 px-5 py-3">
<img class="img-fluid image card-img-top" title="${item[key].title}" src="${item[key].url}" alt="${item[key].title}">
</div>
<div class="card-body text-center">
<h5 class="card-title">${item[key].title}</h5>
<p class="card-text">$${item[key].price}</p>
</div>
<div class="card-footer">
<button type="button" class="addCart btn">ADD TO CART</button>
</div>
</div>
</div>
`;
}
productRow.innerHTML = html;
}
// titleInput.value = '';
// priceInput.value = '';
// inputFile.value = '';
// let delBtn = document.querySelector('#delBtn');
// let tableRow = document.querySelector('#tableRow');
// delBtn.addEventListener('click', function () {
// products.pop();
// tableRow.remove();
// console.log(products);
// mainFunction();
// })