回答編集履歴
2
調整
answer
CHANGED
@@ -156,4 +156,40 @@
|
|
156
156
|
</tr>
|
157
157
|
</tbody>
|
158
158
|
</table>
|
159
|
+
```
|
160
|
+
|
161
|
+
# 特定の列を表示しない
|
162
|
+
ちょうどhideクラスを作ってあるので、それをthやtdに指定してやる
|
163
|
+
場合によってはnth-childなどで指定してもいいが構造が変わるとずれるので微妙
|
164
|
+
```HTML
|
165
|
+
<table id="t1">
|
166
|
+
<thead>
|
167
|
+
<tr>
|
168
|
+
<th>名前</th>
|
169
|
+
<th>色</th>
|
170
|
+
<th>種類</th>
|
171
|
+
<th class="hide">旬</th>
|
172
|
+
</tr>
|
173
|
+
</thead>
|
174
|
+
<tbody>
|
175
|
+
<tr>
|
176
|
+
<td>みかん</td>
|
177
|
+
<td class="color">オレンジ</td>
|
178
|
+
<td class="kind">果物</td>
|
179
|
+
<td class="season hide">冬</td>
|
180
|
+
</tr>
|
181
|
+
<tr>
|
182
|
+
<td>りんご</td>
|
183
|
+
<td class="color">赤</td>
|
184
|
+
<td class="kind">果物</td>
|
185
|
+
<td class="season hide">秋・冬</td>
|
186
|
+
</tr>
|
187
|
+
<tr>
|
188
|
+
<td>にんじん</td>
|
189
|
+
<td class="color">オレンジ</td>
|
190
|
+
<td class="kind">野菜</td>
|
191
|
+
<td class="season hide">冬・春</td>
|
192
|
+
</tr>
|
193
|
+
</tbody>
|
194
|
+
</table>
|
159
195
|
```
|
1
拡張
answer
CHANGED
@@ -72,5 +72,88 @@
|
|
72
72
|
<td class="kind">野菜</td>
|
73
73
|
</tr>
|
74
74
|
</tbody>
|
75
|
-
</table>
|
75
|
+
</table>
|
76
|
+
```
|
77
|
+
|
78
|
+
# 3つ以上拡張
|
79
|
+
部分一致なので、複数季節をかいてもヒットする
|
80
|
+
|
81
|
+
```javascript<style>
|
82
|
+
.hide{display:none;}
|
83
|
+
</style>
|
84
|
+
<script>
|
85
|
+
window.addEventListener('DOMContentLoaded', function(e){
|
86
|
+
var c=["color","kind","season"]; /* ここにクラスを追加する */
|
87
|
+
[].forEach.call(document.querySelectorAll(c.map(function(x){
|
88
|
+
return "."+x;
|
89
|
+
}).join(",")),function(x){
|
90
|
+
x.addEventListener('change',function(e){
|
91
|
+
var search={};
|
92
|
+
c.forEach(function(x){
|
93
|
+
search[x]=[].map.call(document.querySelectorAll('[type=checkbox].'+x+':checked'),function(x){
|
94
|
+
return x.value;
|
95
|
+
});
|
96
|
+
});
|
97
|
+
var len={};
|
98
|
+
var reg={};
|
99
|
+
Object.keys(search).forEach(function(x){
|
100
|
+
len[x]=search[x].length;
|
101
|
+
reg[x]=new RegExp(search[x].join("|"));
|
102
|
+
});
|
103
|
+
[].forEach.call(document.querySelectorAll('#t1 tbody tr'),function(x){
|
104
|
+
var flg=c.map(function(y){
|
105
|
+
if(len[y]==0) return true;
|
106
|
+
return x.querySelector('td.'+y).textContent.match(reg[y])?true:false;
|
107
|
+
}).filter(function(y){
|
108
|
+
return !y;
|
109
|
+
}).length>0;
|
110
|
+
flg?x.classList.add("hide"):x.classList.remove("hide");
|
111
|
+
});
|
112
|
+
});
|
113
|
+
});
|
114
|
+
});
|
115
|
+
</script>
|
116
|
+
<div>色:
|
117
|
+
<label><input type="checkbox" value="オレンジ" class="color">オレンジ</label>
|
118
|
+
<label><input type="checkbox" value="赤" class="color">赤</label>
|
119
|
+
</div>
|
120
|
+
<div>種類:
|
121
|
+
<label><input type="checkbox" value="果物" class="kind">果物</label>
|
122
|
+
<label><input type="checkbox" value="野菜" class="kind">野菜</label>
|
123
|
+
</div>
|
124
|
+
<div>旬:
|
125
|
+
<label><input type="checkbox" value="春" class="season">春</label>
|
126
|
+
<label><input type="checkbox" value="夏" class="season">夏</label>
|
127
|
+
<label><input type="checkbox" value="秋" class="season">秋</label>
|
128
|
+
<label><input type="checkbox" value="冬" class="season">冬</label>
|
129
|
+
</div>
|
130
|
+
<table id="t1">
|
131
|
+
<thead>
|
132
|
+
<tr>
|
133
|
+
<th>名前</th>
|
134
|
+
<th>色</th>
|
135
|
+
<th>種類</th>
|
136
|
+
</tr>
|
137
|
+
</thead>
|
138
|
+
<tbody>
|
139
|
+
<tr>
|
140
|
+
<td>みかん</td>
|
141
|
+
<td class="color">オレンジ</td>
|
142
|
+
<td class="kind">果物</td>
|
143
|
+
<td class="season">冬</td>
|
144
|
+
</tr>
|
145
|
+
<tr>
|
146
|
+
<td>りんご</td>
|
147
|
+
<td class="color">赤</td>
|
148
|
+
<td class="kind">果物</td>
|
149
|
+
<td class="season">秋・冬</td>
|
150
|
+
</tr>
|
151
|
+
<tr>
|
152
|
+
<td>にんじん</td>
|
153
|
+
<td class="color">オレンジ</td>
|
154
|
+
<td class="kind">野菜</td>
|
155
|
+
<td class="season">冬・春</td>
|
156
|
+
</tr>
|
157
|
+
</tbody>
|
158
|
+
</table>
|
76
159
|
```
|