teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

sample

2019/06/04 06:32

投稿

yambejp
yambejp

スコア117906

answer CHANGED
@@ -1,1 +1,218 @@
1
- roadbike.jsを2回呼んでるからじゃないですか?
1
+ roadbike.jsを2回呼んでるからじゃないですか?
2
+
3
+ # リファクタリング
4
+
5
+ ついでなので整理しておきます
6
+ - html
7
+ ```html
8
+ <script>
9
+ window.addEventListener('DOMContentLoaded', function(e){
10
+ document.querySelector('#btn2').disabled=true;
11
+ document.querySelector('form input[name=all]').addEventListener('change',function(e){
12
+ var t=e.target;
13
+ e.target.checked=true;
14
+ [].forEach.call(document.querySelectorAll('form input[name=maker]:checked'),function(x){
15
+ x.checked=false;
16
+ });
17
+ view();
18
+ });
19
+ [].forEach.call(document.querySelectorAll('form input[name=maker]'),function(x){
20
+ x.addEventListener('change',function(e){
21
+ var flg=false;
22
+ const l1=document.querySelectorAll('form input[name=maker]').length;
23
+ const l2=document.querySelectorAll('form input[name=maker]:checked').length;
24
+ if(l1==l2){
25
+ flg=true;
26
+ [].forEach.call(document.querySelectorAll('form input[name=maker]:checked'),function(x){
27
+ x.checked=false;
28
+ });
29
+ }
30
+ document.querySelector('form input[name=all]').checked=flg;
31
+ view();
32
+ });
33
+ });
34
+ document.querySelector('#btn1').addEventListener('click',function(e){
35
+ e.target.disabled=true;
36
+ document.querySelector('#btn2').disabled=false;
37
+ const url="data.json";
38
+ const xhr=new XMLHttpRequest();
39
+ xhr.open( "GET", url);
40
+ xhr.onreadystatechange=function(){
41
+ if(( xhr.readyState == 4 ) && ( xhr.status == 200 )){
42
+ JSON.parse(xhr.response).forEach(function(data){
43
+ const tr=document.createElement('tr');
44
+ Object.values(data).forEach(function(x){
45
+ const td=document.createElement('td');
46
+ td.appendChild(document.createTextNode(x));
47
+ tr.appendChild(td);
48
+ });
49
+ document.querySelector('tbody').appendChild(tr);
50
+ });
51
+ view();
52
+ }
53
+ }
54
+ xhr.send('');
55
+ });
56
+ document.querySelector('#btn2').addEventListener('click',function(e){
57
+ e.target.disabled=true;
58
+ document.querySelector('#btn1').disabled=false;
59
+ [].map.call(document.querySelectorAll('tbody tr'),function(x){
60
+ return x;
61
+ }).forEach(function(x){
62
+ x.parentNode.removeChild(x);
63
+ });
64
+ });
65
+ });
66
+ function view(){
67
+ const vals=[].map.call(document.querySelectorAll('form [name=maker]:checked'),function(x){
68
+ return x.value;
69
+ });
70
+ [].forEach.call(document.querySelectorAll('tbody tr'),function(x){
71
+ var flg=vals.indexOf(x.querySelector('td:nth-child(2)').textContent)==-1;
72
+ if(document.querySelector('form input[name=all]').checked) flg=false;
73
+ x.style.display=flg?"none":"";
74
+ });
75
+ }
76
+ </script>
77
+ <div id="container">
78
+ <header>
79
+ </header>
80
+ <main>
81
+ <form>
82
+ <input type="button" id = "btn1" class="btn" value="挿入"></input>
83
+ <input type="button" id = "btn2" class="btn" value="削除" disabled="true"></input>
84
+ <input type="checkbox" value="all" name="all" checked>ALL</input><br>
85
+ <input type="checkbox" value="scott" name="maker">SCOTT</input><br>
86
+ <input type="checkbox" value="anchor" name="maker">ANCHOR</input><br>
87
+ <input type="checkbox" value="kouta" name="maker">KOUTA</input><br>
88
+ <input type="checkbox" value="pinarello" name="maker">PINARELLO</input><br>
89
+ <input type="checkbox" value="bianchi" name="maker">BIANCHI</input><br>
90
+ <input type="checkbox" value="trek" name="maker">TREK</input><br>
91
+ <input type="checkbox" value="look" name="maker">LOOK</input><br>
92
+ <input type="checkbox" value="ridley" name="maker">RIDLEY</input>
93
+ </form>
94
+ <table>
95
+ <thead>
96
+ <th>バイク名</th>
97
+ <th>メーカー</th>
98
+ <th>フレーム</th>
99
+ <th>コンポ</th>
100
+ <th>価格</th>
101
+ </thead>
102
+ <tbody>
103
+ </tbody>
104
+ </table>
105
+ </main>
106
+ <footer>&copy;タコナキンジ</footer>
107
+ </div>
108
+ ```
109
+ - data.json
110
+ ```json
111
+ [
112
+ {
113
+ "name" : "foil30.1" ,
114
+ "maker" : "scott" ,
115
+ "frame" : "carbon" ,
116
+ "component" : "105" ,
117
+ "price" : "\360,000"
118
+ },
119
+ {
120
+ "name" : "addict rc10" ,
121
+ "maker" : "scott" ,
122
+ "frame" : "carbon" ,
123
+ "component" : "dura-ace" ,
124
+ "price" : "\569,000"
125
+ },
126
+ {
127
+ "name" : "speedstar 10" ,
128
+ "maker" : "scott" ,
129
+ "frame" : "aluminium" ,
130
+ "component" : "105" ,
131
+ "price" : "\149,000"
132
+ },
133
+ {
134
+ "name" : "rs8 elite" ,
135
+ "maker" : "anchor" ,
136
+ "frame" : "carbon" ,
137
+ "component" : "ultegra" ,
138
+ "price" : "\525,000"
139
+ },
140
+ {
141
+ "name" : "rnc3 equipe" ,
142
+ "maker" : "anchor" ,
143
+ "frame" : "Cr&Mo" ,
144
+ "component" : "105" ,
145
+ "price" : "\195,000"
146
+ },
147
+ {
148
+ "name" : "kouger" ,
149
+ "maker" : "kouta" ,
150
+ "frame" : "carbon" ,
151
+ "component" : "ultegra" ,
152
+ "price" : "\517,000"
153
+ },
154
+ {
155
+ "name" : "prince" ,
156
+ "maker" : "pinarello" ,
157
+ "frame" : "carbon" ,
158
+ "component" : "ultegra" ,
159
+ "price" : "\465,000"
160
+ },
161
+ {
162
+ "name" : "angliru" ,
163
+ "maker" : "pinarello" ,
164
+ "frame" : "carbon" ,
165
+ "component" : "105" ,
166
+ "price" : "\243,000"
167
+ },
168
+ {
169
+ "name" : "prima" ,
170
+ "maker" : "pinarello" ,
171
+ "frame" : "aluminium" ,
172
+ "component" : "sora" ,
173
+ "price" : "\149,000"
174
+ },
175
+ {
176
+ "name" : "aria" ,
177
+ "maker" : "bianchi" ,
178
+ "frame" : "carbon" ,
179
+ "component" : "105" ,
180
+ "price" : "\278,000"
181
+ },
182
+ {
183
+ "name" : "impulso" ,
184
+ "maker" : "bianchi" ,
185
+ "frame" : "aluminium" ,
186
+ "component" : "tiagra" ,
187
+ "price" : "\147,000"
188
+ },
189
+ {
190
+ "name" : "madone 9.0" ,
191
+ "maker" : "trek" ,
192
+ "frame" : "carbon" ,
193
+ "component" : "altegra" ,
194
+ "price" : "\510,000"
195
+ },
196
+ {
197
+ "name" : "emonda sl7" ,
198
+ "maker" : "trek" ,
199
+ "frame" : "carbon" ,
200
+ "component" : "ultegra" ,
201
+ "price" : "\482,000"
202
+ },
203
+ {
204
+ "name" : "765 optimum" ,
205
+ "maker" : "look" ,
206
+ "frame" : "carbon" ,
207
+ "component" : "ultegra" ,
208
+ "price" : "\349,800"
209
+ },
210
+ {
211
+ "name" : "fenix c" ,
212
+ "maker" : "ridley" ,
213
+ "frame" : "carbon" ,
214
+ "component" : "105" ,
215
+ "price" : "\260,000"
216
+ }
217
+ ]
218
+ ```