回答編集履歴

1

sample

2019/06/04 06:32

投稿

yambejp
yambejp

スコア114850

test CHANGED
@@ -1 +1,435 @@
1
1
  roadbike.jsを2回呼んでるからじゃないですか?
2
+
3
+
4
+
5
+ # リファクタリング
6
+
7
+
8
+
9
+ ついでなので整理しておきます
10
+
11
+ - html
12
+
13
+ ```html
14
+
15
+ <script>
16
+
17
+ window.addEventListener('DOMContentLoaded', function(e){
18
+
19
+ document.querySelector('#btn2').disabled=true;
20
+
21
+ document.querySelector('form input[name=all]').addEventListener('change',function(e){
22
+
23
+ var t=e.target;
24
+
25
+ e.target.checked=true;
26
+
27
+ [].forEach.call(document.querySelectorAll('form input[name=maker]:checked'),function(x){
28
+
29
+ x.checked=false;
30
+
31
+ });
32
+
33
+ view();
34
+
35
+ });
36
+
37
+ [].forEach.call(document.querySelectorAll('form input[name=maker]'),function(x){
38
+
39
+ x.addEventListener('change',function(e){
40
+
41
+ var flg=false;
42
+
43
+ const l1=document.querySelectorAll('form input[name=maker]').length;
44
+
45
+ const l2=document.querySelectorAll('form input[name=maker]:checked').length;
46
+
47
+ if(l1==l2){
48
+
49
+ flg=true;
50
+
51
+ [].forEach.call(document.querySelectorAll('form input[name=maker]:checked'),function(x){
52
+
53
+ x.checked=false;
54
+
55
+ });
56
+
57
+ }
58
+
59
+ document.querySelector('form input[name=all]').checked=flg;
60
+
61
+ view();
62
+
63
+ });
64
+
65
+ });
66
+
67
+ document.querySelector('#btn1').addEventListener('click',function(e){
68
+
69
+ e.target.disabled=true;
70
+
71
+ document.querySelector('#btn2').disabled=false;
72
+
73
+ const url="data.json";
74
+
75
+ const xhr=new XMLHttpRequest();
76
+
77
+ xhr.open( "GET", url);
78
+
79
+ xhr.onreadystatechange=function(){
80
+
81
+ if(( xhr.readyState == 4 ) && ( xhr.status == 200 )){
82
+
83
+ JSON.parse(xhr.response).forEach(function(data){
84
+
85
+ const tr=document.createElement('tr');
86
+
87
+ Object.values(data).forEach(function(x){
88
+
89
+ const td=document.createElement('td');
90
+
91
+ td.appendChild(document.createTextNode(x));
92
+
93
+ tr.appendChild(td);
94
+
95
+ });
96
+
97
+ document.querySelector('tbody').appendChild(tr);
98
+
99
+ });
100
+
101
+ view();
102
+
103
+ }
104
+
105
+ }
106
+
107
+ xhr.send('');
108
+
109
+ });
110
+
111
+ document.querySelector('#btn2').addEventListener('click',function(e){
112
+
113
+ e.target.disabled=true;
114
+
115
+ document.querySelector('#btn1').disabled=false;
116
+
117
+ [].map.call(document.querySelectorAll('tbody tr'),function(x){
118
+
119
+ return x;
120
+
121
+ }).forEach(function(x){
122
+
123
+ x.parentNode.removeChild(x);
124
+
125
+ });
126
+
127
+ });
128
+
129
+ });
130
+
131
+ function view(){
132
+
133
+ const vals=[].map.call(document.querySelectorAll('form [name=maker]:checked'),function(x){
134
+
135
+ return x.value;
136
+
137
+ });
138
+
139
+ [].forEach.call(document.querySelectorAll('tbody tr'),function(x){
140
+
141
+ var flg=vals.indexOf(x.querySelector('td:nth-child(2)').textContent)==-1;
142
+
143
+ if(document.querySelector('form input[name=all]').checked) flg=false;
144
+
145
+ x.style.display=flg?"none":"";
146
+
147
+ });
148
+
149
+ }
150
+
151
+ </script>
152
+
153
+ <div id="container">
154
+
155
+ <header>
156
+
157
+ </header>
158
+
159
+ <main>
160
+
161
+ <form>
162
+
163
+ <input type="button" id = "btn1" class="btn" value="挿入"></input>
164
+
165
+ <input type="button" id = "btn2" class="btn" value="削除" disabled="true"></input>
166
+
167
+ <input type="checkbox" value="all" name="all" checked>ALL</input><br>
168
+
169
+ <input type="checkbox" value="scott" name="maker">SCOTT</input><br>
170
+
171
+ <input type="checkbox" value="anchor" name="maker">ANCHOR</input><br>
172
+
173
+ <input type="checkbox" value="kouta" name="maker">KOUTA</input><br>
174
+
175
+ <input type="checkbox" value="pinarello" name="maker">PINARELLO</input><br>
176
+
177
+ <input type="checkbox" value="bianchi" name="maker">BIANCHI</input><br>
178
+
179
+ <input type="checkbox" value="trek" name="maker">TREK</input><br>
180
+
181
+ <input type="checkbox" value="look" name="maker">LOOK</input><br>
182
+
183
+ <input type="checkbox" value="ridley" name="maker">RIDLEY</input>
184
+
185
+ </form>
186
+
187
+ <table>
188
+
189
+ <thead>
190
+
191
+ <th>バイク名</th>
192
+
193
+ <th>メーカー</th>
194
+
195
+ <th>フレーム</th>
196
+
197
+ <th>コンポ</th>
198
+
199
+ <th>価格</th>
200
+
201
+ </thead>
202
+
203
+ <tbody>
204
+
205
+ </tbody>
206
+
207
+ </table>
208
+
209
+ </main>
210
+
211
+ <footer>&copy;タコナキンジ</footer>
212
+
213
+ </div>
214
+
215
+ ```
216
+
217
+ - data.json
218
+
219
+ ```json
220
+
221
+ [
222
+
223
+ {
224
+
225
+ "name" : "foil30.1" ,
226
+
227
+ "maker" : "scott" ,
228
+
229
+ "frame" : "carbon" ,
230
+
231
+ "component" : "105" ,
232
+
233
+ "price" : "\360,000"
234
+
235
+ },
236
+
237
+ {
238
+
239
+ "name" : "addict rc10" ,
240
+
241
+ "maker" : "scott" ,
242
+
243
+ "frame" : "carbon" ,
244
+
245
+ "component" : "dura-ace" ,
246
+
247
+ "price" : "\569,000"
248
+
249
+ },
250
+
251
+ {
252
+
253
+ "name" : "speedstar 10" ,
254
+
255
+ "maker" : "scott" ,
256
+
257
+ "frame" : "aluminium" ,
258
+
259
+ "component" : "105" ,
260
+
261
+ "price" : "\149,000"
262
+
263
+ },
264
+
265
+ {
266
+
267
+ "name" : "rs8 elite" ,
268
+
269
+ "maker" : "anchor" ,
270
+
271
+ "frame" : "carbon" ,
272
+
273
+ "component" : "ultegra" ,
274
+
275
+ "price" : "\525,000"
276
+
277
+ },
278
+
279
+ {
280
+
281
+ "name" : "rnc3 equipe" ,
282
+
283
+ "maker" : "anchor" ,
284
+
285
+ "frame" : "Cr&Mo" ,
286
+
287
+ "component" : "105" ,
288
+
289
+ "price" : "\195,000"
290
+
291
+ },
292
+
293
+ {
294
+
295
+ "name" : "kouger" ,
296
+
297
+ "maker" : "kouta" ,
298
+
299
+ "frame" : "carbon" ,
300
+
301
+ "component" : "ultegra" ,
302
+
303
+ "price" : "\517,000"
304
+
305
+ },
306
+
307
+ {
308
+
309
+ "name" : "prince" ,
310
+
311
+ "maker" : "pinarello" ,
312
+
313
+ "frame" : "carbon" ,
314
+
315
+ "component" : "ultegra" ,
316
+
317
+ "price" : "\465,000"
318
+
319
+ },
320
+
321
+ {
322
+
323
+ "name" : "angliru" ,
324
+
325
+ "maker" : "pinarello" ,
326
+
327
+ "frame" : "carbon" ,
328
+
329
+ "component" : "105" ,
330
+
331
+ "price" : "\243,000"
332
+
333
+ },
334
+
335
+ {
336
+
337
+ "name" : "prima" ,
338
+
339
+ "maker" : "pinarello" ,
340
+
341
+ "frame" : "aluminium" ,
342
+
343
+ "component" : "sora" ,
344
+
345
+ "price" : "\149,000"
346
+
347
+ },
348
+
349
+ {
350
+
351
+ "name" : "aria" ,
352
+
353
+ "maker" : "bianchi" ,
354
+
355
+ "frame" : "carbon" ,
356
+
357
+ "component" : "105" ,
358
+
359
+ "price" : "\278,000"
360
+
361
+ },
362
+
363
+ {
364
+
365
+ "name" : "impulso" ,
366
+
367
+ "maker" : "bianchi" ,
368
+
369
+ "frame" : "aluminium" ,
370
+
371
+ "component" : "tiagra" ,
372
+
373
+ "price" : "\147,000"
374
+
375
+ },
376
+
377
+ {
378
+
379
+ "name" : "madone 9.0" ,
380
+
381
+ "maker" : "trek" ,
382
+
383
+ "frame" : "carbon" ,
384
+
385
+ "component" : "altegra" ,
386
+
387
+ "price" : "\510,000"
388
+
389
+ },
390
+
391
+ {
392
+
393
+ "name" : "emonda sl7" ,
394
+
395
+ "maker" : "trek" ,
396
+
397
+ "frame" : "carbon" ,
398
+
399
+ "component" : "ultegra" ,
400
+
401
+ "price" : "\482,000"
402
+
403
+ },
404
+
405
+ {
406
+
407
+ "name" : "765 optimum" ,
408
+
409
+ "maker" : "look" ,
410
+
411
+ "frame" : "carbon" ,
412
+
413
+ "component" : "ultegra" ,
414
+
415
+ "price" : "\349,800"
416
+
417
+ },
418
+
419
+ {
420
+
421
+ "name" : "fenix c" ,
422
+
423
+ "maker" : "ridley" ,
424
+
425
+ "frame" : "carbon" ,
426
+
427
+ "component" : "105" ,
428
+
429
+ "price" : "\260,000"
430
+
431
+ }
432
+
433
+ ]
434
+
435
+ ```