質問編集履歴
1
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -52,10 +52,634 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
55
61
|
ーー参考資料ーーー
|
56
62
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
63
|
+
htmlファイル
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
<!doctype html>
|
68
|
+
|
69
|
+
<html>
|
70
|
+
|
71
|
+
<head>
|
72
|
+
|
73
|
+
<meta charset="utf-8">
|
74
|
+
|
75
|
+
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
76
|
+
|
77
|
+
<title>絞り込み検索サンプル</title>
|
78
|
+
|
79
|
+
<link rel="stylesheet" type="text/css" href="css/style.css">
|
80
|
+
|
81
|
+
</head>
|
82
|
+
|
83
|
+
<body>
|
84
|
+
|
85
|
+
<div class="filterArea">
|
86
|
+
|
87
|
+
<h1>絞り込み検索 条件設定</h1>
|
88
|
+
|
89
|
+
<div class="filter_life">
|
90
|
+
|
91
|
+
<h2>平均寿命</h2>
|
92
|
+
|
93
|
+
<select name="life">
|
94
|
+
|
95
|
+
<option value="">指定なし</option>
|
96
|
+
|
97
|
+
<option value="1">~1</option>
|
98
|
+
|
99
|
+
<option value="2">2~20</option>
|
100
|
+
|
101
|
+
<option value="3">31~50</option>
|
102
|
+
|
103
|
+
<option value="4">51~</option>
|
104
|
+
|
105
|
+
</select>
|
106
|
+
|
107
|
+
</div>
|
108
|
+
|
109
|
+
<div class="filter_tag">
|
110
|
+
|
111
|
+
<h2>属性</h2>
|
112
|
+
|
113
|
+
<input type="checkbox" name="tag" value="哺乳類" id="chk01"><label for="chk01">哺乳類</label>
|
114
|
+
|
115
|
+
<input type="checkbox" name="tag" value="鳥類" id="chk02"><label for="chk02">鳥類</label>
|
116
|
+
|
117
|
+
<input type="checkbox" name="tag" value="魚類" id="chk03"><label for="chk03">魚類</label>
|
118
|
+
|
119
|
+
<input type="checkbox" name="tag" value="昆虫類" id="chk04"><label for="chk04">昆虫類</label>
|
120
|
+
|
121
|
+
<input type="checkbox" name="tag" value="かわいい" id="chk05"><label for="chk05">かわいい</label>
|
122
|
+
|
123
|
+
<input type="checkbox" name="tag" value="かっこいい" id="chk06"><label for="chk06">かっこいい</label>
|
124
|
+
|
125
|
+
<input type="checkbox" name="tag" value="ほのぼの" id="chk07"><label for="chk07">ほのぼの</label>
|
126
|
+
|
127
|
+
<input type="checkbox" name="tag" value="こわい" id="chk08"><label for="chk08">こわい</label>
|
128
|
+
|
129
|
+
<input type="checkbox" name="tag" value="うごかない" id="chk09"><label for="chk09">うごかない</label>
|
130
|
+
|
131
|
+
<input type="checkbox" name="tag" value="だれなん?" id="chk10"><label for="chk10">だれなん?</label>
|
132
|
+
|
133
|
+
</div>
|
134
|
+
|
135
|
+
<div class="filter_keyword">
|
136
|
+
|
137
|
+
<h2>キーワード</h2>
|
138
|
+
|
139
|
+
<input type="text" name="filter_free" size="40" /> <button type='button' name='free_submit'>検索</button>
|
140
|
+
|
141
|
+
</div>
|
142
|
+
|
143
|
+
<!--/.filterArea--></div>
|
144
|
+
|
145
|
+
<div class="resultArea">
|
146
|
+
|
147
|
+
<div class="productCntArea"></div>
|
148
|
+
|
149
|
+
<div class="productArea"></div>
|
150
|
+
|
151
|
+
<!--/.resultArea--></div>
|
152
|
+
|
153
|
+
<script src="js/jquery.min.js"></script>
|
154
|
+
|
155
|
+
<script src="js/underscore-min.js"></script>
|
156
|
+
|
157
|
+
<script src="js/script.js"></script>
|
158
|
+
|
159
|
+
</body>
|
160
|
+
|
161
|
+
</html>
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
jsファイル
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
$(function() {
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
/*================================================================
|
186
|
+
|
187
|
+
情報
|
188
|
+
|
189
|
+
================================================================*/
|
190
|
+
|
191
|
+
var allList = [
|
192
|
+
|
193
|
+
{
|
194
|
+
|
195
|
+
id: "id001",
|
196
|
+
|
197
|
+
life: 14,
|
198
|
+
|
199
|
+
title: "猫",
|
200
|
+
|
201
|
+
tag: ["哺乳類","かわいい","かっこいい"],
|
202
|
+
|
203
|
+
description: "猫様は何をしても許されます。猫様にあやかりましょう。目指せ閲覧数アップ。"
|
204
|
+
|
205
|
+
},
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
id: "id002",
|
210
|
+
|
211
|
+
life: 36,
|
212
|
+
|
213
|
+
title: "ハシビロコウ",
|
214
|
+
|
215
|
+
tag: ["鳥類","かっこいい","こわい","うごかない"],
|
216
|
+
|
217
|
+
description: "(以下Wikipediaより)ペリカン目ハシビロコウ科の鳥類の一種である。 本種のみでハシビロコウ科を形成する(1属1種)。"
|
218
|
+
|
219
|
+
},
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
id: "id003",
|
224
|
+
|
225
|
+
life: 50,
|
226
|
+
|
227
|
+
title: "ハタ(お魚)",
|
228
|
+
|
229
|
+
tag: ["魚類","ほのぼの"],
|
230
|
+
|
231
|
+
description: "(以下Wikipediaより)スズキ目ハタ科ハタ亜科 Epinephelinae に属する魚の総称。ハタ亜科はマハタ亜科とよぶ場合もある。"
|
232
|
+
|
233
|
+
},
|
234
|
+
|
235
|
+
{
|
236
|
+
|
237
|
+
id: "id004",
|
238
|
+
|
239
|
+
life: 50,
|
240
|
+
|
241
|
+
title: "ダチョウ",
|
242
|
+
|
243
|
+
tag: ["鳥類","かっこいい","こわい"],
|
244
|
+
|
245
|
+
description: "(以下Wikipediaより)ダチョウ目ダチョウ科ダチョウ属に分類される鳥。鳥でありながら飛ぶことは出来ず、平胸類に分類される(飛べない鳥も参照)。"
|
246
|
+
|
247
|
+
},
|
248
|
+
|
249
|
+
{
|
250
|
+
|
251
|
+
id: "id005",
|
252
|
+
|
253
|
+
life: 0.5,
|
254
|
+
|
255
|
+
title: "カマキリ",
|
256
|
+
|
257
|
+
tag: ["昆虫類","かっこいい","こわい"],
|
258
|
+
|
259
|
+
description: "(以下Wikipediaより)昆虫綱カマキリ目(蟷螂目、学名:Mantodea)に分類される昆虫の総称。前脚が鎌状に変化し、他の小動物を捕食する肉食性の昆虫である。"
|
260
|
+
|
261
|
+
},
|
262
|
+
|
263
|
+
{
|
264
|
+
|
265
|
+
id: "id006",
|
266
|
+
|
267
|
+
life: 80,
|
268
|
+
|
269
|
+
title: "おじさん",
|
270
|
+
|
271
|
+
tag: ["哺乳類","だれなん?"],
|
272
|
+
|
273
|
+
description: "だれ!?"
|
274
|
+
|
275
|
+
},
|
276
|
+
|
277
|
+
{
|
278
|
+
|
279
|
+
id: "id007",
|
280
|
+
|
281
|
+
life: 10,
|
282
|
+
|
283
|
+
title: "リス",
|
284
|
+
|
285
|
+
tag: ["哺乳類","かわいい","ほのぼの"],
|
286
|
+
|
287
|
+
description: "(以下Wikipediaより)ネズミ目(齧歯目)リス科に属する動物の総称である。"
|
288
|
+
|
289
|
+
},
|
290
|
+
|
291
|
+
{
|
292
|
+
|
293
|
+
id: "id008",
|
294
|
+
|
295
|
+
life: 20,
|
296
|
+
|
297
|
+
title: "フクロウ",
|
298
|
+
|
299
|
+
tag: ["鳥類","こわい"],
|
300
|
+
|
301
|
+
description: "(以下Wikipediaより)フクロウ目フクロウ科フクロウ属に分類される猛禽類である鳥類の一種。"
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
];
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
/*================================================================
|
310
|
+
|
311
|
+
スクリプトはじまり
|
312
|
+
|
313
|
+
================================================================*/
|
314
|
+
|
315
|
+
function init() {
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
//イベント登録
|
320
|
+
|
321
|
+
$(".filter_life select").on("change", onFilterChange);
|
322
|
+
|
323
|
+
$(".filter_tag input").on("change", onFilterChange);
|
324
|
+
|
325
|
+
$(".filter_keyword button").on("click", onFilterChange);
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
//最初は全て出力
|
332
|
+
|
333
|
+
refleshHtml(allList);
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
/*================================================================
|
340
|
+
|
341
|
+
HTML出力
|
342
|
+
|
343
|
+
================================================================*/
|
344
|
+
|
345
|
+
function refleshHtml(list) {
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
var outputHtml = '';
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
//出力する内容をoutputHtmlに格納
|
354
|
+
|
355
|
+
if (list.length > 0) {
|
356
|
+
|
357
|
+
_.each(list, function(line, i) {
|
358
|
+
|
359
|
+
outputHtml += '<div class="product">';
|
360
|
+
|
361
|
+
outputHtml += ' <div class="photo"><img src="images/' + line.id + '.jpg" alt="' + line.title + '" /></div>';
|
362
|
+
|
363
|
+
outputHtml += ' <div class="info">';
|
364
|
+
|
365
|
+
outputHtml += ' <h3 class="productTitle">' + line.title + '</h3>';
|
366
|
+
|
367
|
+
outputHtml += ' <p class="productLife">平均寿命(推定): ' + line.life + '年</p>';
|
368
|
+
|
369
|
+
outputHtml += ' <ul class="tag">';
|
370
|
+
|
371
|
+
_.each(line.tag, function(tag, i){
|
372
|
+
|
373
|
+
outputHtml += ' <li>' + tag + '</li>';
|
374
|
+
|
375
|
+
});
|
376
|
+
|
377
|
+
outputHtml += ' </ul>';
|
378
|
+
|
379
|
+
outputHtml += ' <p class="description">' + line.description + '</p>';
|
380
|
+
|
381
|
+
outputHtml += ' </div>';
|
382
|
+
|
383
|
+
outputHtml += '<!--/.product--></div>';
|
384
|
+
|
385
|
+
});
|
386
|
+
|
387
|
+
} else {
|
388
|
+
|
389
|
+
outputHtml += '<div class="noproduct"><p>条件に当てはまる商品が見つかりません。</p></div>';
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
//HTML出力(フェードインアニメーションつき)
|
396
|
+
|
397
|
+
$('.productArea').html(outputHtml);
|
398
|
+
|
399
|
+
$('.productArea .product').css({opacity: 0}).each(function(i){$(this).delay(100 * i).animate({opacity:1}, 300);
|
400
|
+
|
401
|
+
});
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
//検索件数表示
|
406
|
+
|
407
|
+
$('.productCntArea').html('<p>' + allList.length + '件中 ' + list.length + '件</p>');
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
/*================================================================
|
414
|
+
|
415
|
+
絞り込み条件を変更した時
|
416
|
+
|
417
|
+
================================================================*/
|
418
|
+
|
419
|
+
function onFilterChange(e) {
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
var filterFncs = [];
|
424
|
+
|
425
|
+
var result = [];
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
//セレクトボックスの値を引数に指定した関数filterByLifeをfilterFuncs配列に格納
|
430
|
+
|
431
|
+
filterFncs.push(
|
432
|
+
|
433
|
+
function(list) {
|
434
|
+
|
435
|
+
return filterByLife(list, $('.filter_life select').val());
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
);
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
//チェックボックスの値を引数に指定した関数filterByTagをfilterFuncs配列に格納
|
444
|
+
|
445
|
+
filterFncs.push(
|
446
|
+
|
447
|
+
function(list) {
|
448
|
+
|
449
|
+
return filterByTag(list, $('.filter_tag input:checked'));
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
);
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
//キーワードの値を引数に指定した関数filterByKeywordをfilterFuncs配列に格納
|
458
|
+
|
459
|
+
filterFncs.push(
|
460
|
+
|
461
|
+
function(list) {
|
462
|
+
|
463
|
+
return filterByKeyword(list, _.escape($('.filter_keyword input').val()));
|
464
|
+
|
465
|
+
}
|
466
|
+
|
467
|
+
);
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
//FilterFuncs配列内の関数をバケツリレーみたいに1つずつ実行して結果をresult配列に格納
|
472
|
+
|
473
|
+
result = _.reduce(filterFncs, function(list, fnc) {
|
474
|
+
|
475
|
+
return fnc(list);
|
476
|
+
|
477
|
+
}, allList);
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
//絞り込んだ結果を出力
|
482
|
+
|
483
|
+
refleshHtml(result);
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
/*================================================================
|
490
|
+
|
491
|
+
絞り込み[1] セレクトボックス絞り込み関数
|
492
|
+
|
493
|
+
================================================================*/
|
494
|
+
|
495
|
+
function filterByLife(list, value) {
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
//絞り込み指定がない場合はリターン
|
500
|
+
|
501
|
+
if (value == "") {
|
502
|
+
|
503
|
+
return list;
|
504
|
+
|
505
|
+
}
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
//選択したセレクトボックスとlifeがマッチするかでフィルタリング
|
510
|
+
|
511
|
+
return _.filter(list, function(item) {
|
512
|
+
|
513
|
+
switch (value) {
|
514
|
+
|
515
|
+
case '1':
|
516
|
+
|
517
|
+
return item.life <= 1;
|
518
|
+
|
519
|
+
case '2':
|
520
|
+
|
521
|
+
return 1 < item.life && item.life <= 20;
|
522
|
+
|
523
|
+
case '3':
|
524
|
+
|
525
|
+
return 20 < item.life && item.life <= 50;
|
526
|
+
|
527
|
+
case '4':
|
528
|
+
|
529
|
+
return 50 < item.life;
|
530
|
+
|
531
|
+
}
|
532
|
+
|
533
|
+
});
|
534
|
+
|
535
|
+
}
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
/*================================================================
|
540
|
+
|
541
|
+
絞り込み[2] チェックボックス絞り込み関数
|
542
|
+
|
543
|
+
================================================================*/
|
544
|
+
|
545
|
+
function filterByTag(list, value) {
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
//絞り込み指定がない場合はリターン
|
550
|
+
|
551
|
+
if (value.length == 0) {
|
552
|
+
|
553
|
+
return list;
|
554
|
+
|
555
|
+
}
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
//選択した属性(チェックボックス)とtagがマッチするかでフィルタリング
|
560
|
+
|
561
|
+
return _.filter(list, function(item) {
|
562
|
+
|
563
|
+
|
564
|
+
|
565
|
+
var isMatch = false;
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
//配列同士の比較
|
570
|
+
|
571
|
+
_.each(value, function(chkItem, i) {
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
_.each(item.tag, function(tagItem, i) {
|
576
|
+
|
577
|
+
if (tagItem === $(chkItem).val()) {
|
578
|
+
|
579
|
+
isMatch = true;
|
580
|
+
|
581
|
+
}
|
582
|
+
|
583
|
+
});
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
});
|
588
|
+
|
589
|
+
|
590
|
+
|
591
|
+
return isMatch;
|
592
|
+
|
593
|
+
});
|
594
|
+
|
595
|
+
}
|
596
|
+
|
597
|
+
|
598
|
+
|
599
|
+
/*================================================================
|
600
|
+
|
601
|
+
絞り込み[3] テキストボックス絞り込み関数
|
602
|
+
|
603
|
+
================================================================*/
|
604
|
+
|
605
|
+
function filterByKeyword(list, value) {
|
606
|
+
|
607
|
+
|
608
|
+
|
609
|
+
//絞り込み指定がない場合はリターン
|
610
|
+
|
611
|
+
if (value == "") {
|
612
|
+
|
613
|
+
return list;
|
614
|
+
|
615
|
+
}
|
616
|
+
|
617
|
+
|
618
|
+
|
619
|
+
//検索キーワードを配列に格納(スペースがある場合は複数格納)
|
620
|
+
|
621
|
+
var freeAry = [];
|
622
|
+
|
623
|
+
var val = value.replace(/ /g, " ");
|
624
|
+
|
625
|
+
searchAry = val.split(" ");
|
626
|
+
|
627
|
+
|
628
|
+
|
629
|
+
//入力したキーワードがtitleもしくdescriptionにマッチするかでフィルタリング
|
630
|
+
|
631
|
+
return _.filter(list, function(item) {
|
632
|
+
|
633
|
+
|
634
|
+
|
635
|
+
var isMatch = false;
|
636
|
+
|
637
|
+
|
638
|
+
|
639
|
+
_.each(searchAry, function(data, i) {
|
640
|
+
|
641
|
+
if (item.title.indexOf(data) != -1 || item.description.indexOf(data) != -1) {
|
642
|
+
|
643
|
+
isMatch = true;
|
644
|
+
|
645
|
+
}
|
646
|
+
|
647
|
+
});
|
648
|
+
|
649
|
+
|
650
|
+
|
651
|
+
return isMatch;
|
652
|
+
|
653
|
+
|
654
|
+
|
655
|
+
});
|
656
|
+
|
657
|
+
|
658
|
+
|
659
|
+
}
|
660
|
+
|
661
|
+
|
662
|
+
|
663
|
+
/*================================================================
|
664
|
+
|
665
|
+
スクリプトはじめ
|
666
|
+
|
667
|
+
================================================================*/
|
668
|
+
|
669
|
+
init();
|
670
|
+
|
671
|
+
|
672
|
+
|
673
|
+
});
|
674
|
+
|
675
|
+
|
676
|
+
|
677
|
+
```
|
678
|
+
|
679
|
+
|
680
|
+
|
681
|
+
|
682
|
+
|
683
|
+
その他、underscore-min.jsというファイルもあります。
|
684
|
+
|
685
|
+
アドバイスに必要なファイルがあればおっしゃってください。
|