-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_jq_menu_table_tin.html
295 lines (263 loc) · 12 KB
/
01_jq_menu_table_tin.html
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid mt-3">
<h2 id="myH2">HEADER</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. </p>
<table class="table table-hover table-striped table-bordered border-primary">
<thead class="text-center myH2">
<tr>
<th width="5%">品項</th>
<th width="8%">圖片</th>
<th width="15%">價格</th>
<th width="15%">數量</th>
<th>小計</th>
</tr>
</thead>
<tbody class="text-center myTbody">
<tr>
<td>
<div class="mt-5">1</div>
</td>
<td>
<div class="mt-4">
<img src="https://picsum.photos/id/42/100/100" alt="">
</div>
</td>
<td>
<div class="mt-4">
<input type="number" class="form-control menu-price" value="100" readonly>
</div>
</td>
<td>
<div class="mt-4">
<input type="number" class="form-control menu-num" value="0" min="0">
</div>
</td>
<td>
<div class="mt-4">
<input type="number" class="form-control menu-total" value="0">
</div>
</td>
</tr>
<tr>
<td>
<div class="mt-4">2</div>
</td>
<td>
<div class="">
<img src="https://picsum.photos/id/30/100/100" alt="">
</div>
</td>
<td>
<div class="">
<input type="number" class="form-control menu-price" value="200" readonly>
</div>
</td>
<td>
<div class="">
<input type="number" class="form-control menu-num" value="0" min="0">
</div>
</td>
<td>
<div class="">
<input type="number" class="form-control menu-total" value="0">
</div>
</td>
</tr>
<tr>
<td>
<div class="mt-4">3</div>
</td>
<td>
<div class="">
<img src="https://picsum.photos/id/3/100/100" alt="">
</div>
</td>
<td>
<div class="">
<input type="number" class="form-control menu-price" value="300" readonly>
</div>
</td>
<td>
<div class="">
<input type="number" class="form-control menu-num" value="0" min="0">
</div>
</td>
<td>
<div class="">
<input type="number" class="form-control menu-total" value="0">
</div>
</td>
</tr>
<tr>
<td colspan="5" height="100px">
<div class="mt-4">
小計:total加總 = <span id="myResult"></span>元
</div>
<div class="mt-4">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal"
data-bs-target="#exampleModal">
calculate
</button>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="mt-2">合計:</div>
</td>
<!-- <td>
</td> -->
<td colspan="3">
<div class="mt-2" id="myResult">加總結果</div>
</td>
<!-- <td>
</td>
<td>
</td> -->
</tr>
</tbody>
</table>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
total: <span id="modalResult">12345</span>
<div id="bonus" class="text-danger">
666
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<div class="d-grid gap-2">
<button class="btn btn-primary btn-lg" type="button" id="myBtn">加總計算</button>
</div>
<br>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"
integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
$(document).ready(function () {
// 1.綁定
const myPrice = $('.menu-price');
const myNum = $('.menu-num');
const myTotal = $('.menu-total');
const myResult = $('#myResult');
const modalResult = $('#modalResult');
const bonus = $('#bonus');
const myBtn = $('#myBtn');
// console.log('myPrice', myPrice);
// console.log('myNum', myNum);
// console.log('myTotal', myTotal);
// console.log('myBtn', myBtn);
console.log('myResult', myResult);
console.log('modalResult', modalResult);
// 預設display none -> jquery hide()
bonus.hide();
//宣告gstSum()函式
//全部加總
// 1. 取得 全部的total -> sum
// 2. sum -> myResult text
// 3. 重構 ->重複程式碼 變成function
// 4. 呼叫函式 -> getSum();
function getSum() {
let sum = 0;
$.each(myTotal, function (indexInArray, valueOfElement) {
console.log('indexInArray', indexInArray);
console.log('valueOfElement', valueOfElement);
let totalVaule = Number($(valueOfElement).val());
sum = sum + totalVaule;
console.log('sum', sum);
});
myResult.text(sum);
modalResult.text(sum);
bonus.hide();
if (sum > 300) {
let bonusValue = sum * 0.8;
bonus.text(`恭喜您 超過300 獲得 優惠八折 => ${bonusValue}`);
// 顯示bonus
bonus.show();
}
}
// 不能一開始就呼叫getSum(),要等myNum的change事件開始後再呼叫
// getSum();
// 2.宣告myNum的change事件
// myNum.change(function () {
// 原生js
// console.log('myNum', myNum);
// console.log('this', this);
//JQ Object
// console.log('$(this)', $(this));
$(myNum).each(function (index, element) {
// element == this
// console.log('index', index);
// console.log('element', element);
$(element).change(function () {
// $(this)表示滑鼠點擊到的數量.menu-num Class
console.log('this', $(this));
let numValue = $(this).val();
console.log('numValue', numValue);
// console.log('parent.parent.parent.find-price', $(this).parent().parent().parent().find('.menu-price'));
// console.log('parent.parent.parent.find-total', $(this).parent().parent().parent().find('.menu-total'));
let prices = $(this).parent().parent().parent().find('.menu-price');
let totals = $(this).parent().parent().parent().find('.menu-total');
console.log('prices', prices);
// console.log('totalValue', totalValue);
sum = Number(prices.val()) * Number($(this).val());
console.log('sum', sum);
totals.val(sum);
console.log('myTotal.val()', myTotal.val());
getSum();
// // 3.宣告myBtn的click事件
// let total = 0;
// $.each(myTotal, function (key, value) {
// $('#myBtn').click(function () {
// // console.log('myBtn OK');
// console.log('$(this)', $(this));
// console.log('value', value);
// myTotalValue = Number($(value).val());
// console.log('myTotalValue', myTotalValue);
// total = total + Number(myTotalValue);
// console.log('total', total);
// });
// myResult.text(total);
// });
// jquery method parent() 找上一層 推薦使用
// jquery method parents() 找上面全部 少用
// jquery method parentsUntil('tr') 找tr以下全部 更少用
// jquery method find('.menu-price') 往下找menu-price class
// console.log('parent', $(this).parent());
// console.log('parent.parent', $(this).parent().parent());
// console.log('parent.parent.parent', $(this).parent().parent().parent());
// console.log('parent.parent.parent.find-price', $(this).parent().parent().parent().find('.menu-price'));
// console.log('parent.parent.parent.find-total', $(this).parent().parent().parent().find('.menu-total'));
// console.log('parentsUntil(tr)', $(this).parentsUntil('tr'));
// console.log('parentsUntil(tr).parent', $(this).parentsUntil('tr').parent());
// console.log('parentsUntil(tbody)', $(this).parentsUntil('tbody'));
});
});
});
</script>
</body>
</html>