$(function() { /*================================================================ 情報 ================================================================*/ var allList = [ { id: "1970", life: "148.8", url:"index.php?act=staff&id=1970", img:"user_data/11092.jpg", nen:"平成29年式", soukou:"52695", title:"【販3末】トヨタシエンタX白6750", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2019", life: "98.8", url:"index.php?act=staff&id=2019", img:"user_data/12847.jpg", nen:"平成27年式", soukou:"57295", title:"カローラフィルダー1.8Sエアロツアラー", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2045", life: "228.8", url:"index.php?act=staff&id=2045", img:"user_data/12964.jpg", nen:"令和2年式", soukou:"24672", title:"トヨタライズZ白", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2056", life: "228.8", url:"index.php?act=staff&id=2056", img:"user_data/13137.jpg", nen:"令和1年式", soukou:"21510", title:"トヨタライズG白6790", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2057", life: "228.8", url:"index.php?act=staff&id=2057", img:"user_data/13134.jpg", nen:"令和1年式", soukou:"38949", title:"トヨタライズZ水色", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2063", life: "238.8", url:"index.php?act=staff&id=2063", img:"user_data/13138.jpg", nen:"令和2年式", soukou:"14200", title:"ダイハツロッキーG青", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2066", life: "178.8", url:"index.php?act=staff&id=2066", img:"user_data/13163.jpg", nen:"平成29年式", soukou:"27858", title:"カローラフィールダーHV G", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2068", life: "178.8", url:"index.php?act=staff&id=2068", img:"user_data/13158.jpg", nen:"平成28年式", soukou:"48157", title:"カローラフィールダーHV Gダブルバイビー", sold:"", new:"", tag: ["","","","AT","売り切れ"], description: "tesut" }, { id: "2072", life: "58.8", url:"index.php?act=staff&id=2072", img:"user_data/13162.jpg", nen:"平成22年式", soukou:"58229", title:"カローラフィルダー1.5X", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2082", life: "128.8", url:"index.php?act=staff&id=2082", img:"user_data/13177.jpg", nen:"平成3年式", soukou:"85,540", title:"スズキジムニー11V ワイルドウインドリミテッド 4WD", sold:"", new:"", tag: ["","","MT","","売り切れ"], description: "tesut" }, { id: "1647", life: "88.8", url:"index.php?act=staff&id=1647", img:"user_data/10710.jpg", nen:"平成26年式", soukou:"55361", title:"【ツDP楽】トヨタカローラフィルダG HV6315", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "1404", life: "58.8", url:"index.php?act=staff&id=1404", img:"user_data/9604.jpg", nen:"平成22年式", soukou:"81254", title:"アイシスプラタナリミテッド924仕入→埼玉", sold:"", new:"", tag: ["","","","AT","売り切れ"], description: "tesut" }, { id: "1380", life: "168.8", url:"index.php?act=staff&id=1380", img:"user_data/9580.jpg", nen:"平成30年式", soukou:"50390", title:"【カ単禁】フリードGホンダセンシング6168", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "1184", life: "138.8", url:"index.php?act=staff&id=1184", img:"user_data/7927.jpg", nen:"平成30年式", soukou:"31410", title:"【ツDP楽】カローラフィールダー黒1.5X5670", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, ]; /*================================================================ スクリプトはじまり ================================================================*/ function init() { //イベント登録 $(".filter_life select").on("change", onFilterChange); $(".filter_tag input").on("change", onFilterChange); $(".filter_keyword button").on("click", onFilterChange); //最初は全て出力 refleshHtml(allList); } /*================================================================ HTML出力 ================================================================*/ function refleshHtml(list) { var outputHtml = ''; //出力する内容をoutputHtmlに格納 if (list.length > 0) { _.each(list, function(line, i) { outputHtml += '

' + line.title + '

'; outputHtml += '' + line.title + ''; outputHtml += '' + line.new + '' + line.sold + ''; outputHtml += '

価格 ' + line.life + '万円

'; outputHtml += '

年式: ' + line.nen + '

'; outputHtml += '

走行: 約 ' + line.soukou + 'km

'; outputHtml += '
'; }); } else { outputHtml += '

条件に当てはまる車を検索できませんでした。

'; } //HTML出力(フェードインアニメーションつき) $('.productArea').html(outputHtml); $('.productArea .product').css({opacity: 0}).each(function(i){$(this).delay(100 * i).animate({opacity:1}, 300); }); //検索件数表示 $('.productCntArea').html('

' + allList.length + '件中' + list.length + '件を表示しています。

'); } /*================================================================ 絞り込み条件を変更した時 ================================================================*/ function onFilterChange(e) { var filterFncs = []; var result = []; //セレクトボックスの値を引数に指定した関数filterByLifeをfilterFuncs配列に格納 filterFncs.push( function(list) { return filterByLife(list, $('.filter_life select').val()); } ); //チェックボックスの値を引数に指定した関数filterByTagをfilterFuncs配列に格納 filterFncs.push( function(list) { return filterByTag(list, $('.filter_tag input:checked')); } ); //キーワードの値を引数に指定した関数filterByKeywordをfilterFuncs配列に格納 filterFncs.push( function(list) { return filterByKeyword(list, _.escape($('.filter_keyword input').val())); } ); //FilterFuncs配列内の関数をバケツリレーみたいに1つずつ実行して結果をresult配列に格納 result = _.reduce(filterFncs, function(list, fnc) { return fnc(list); }, allList); //絞り込んだ結果を出力 refleshHtml(result); } /*================================================================ 絞り込み[1] セレクトボックス絞り込み関数 ================================================================*/ function filterByLife(list, value) { //絞り込み指定がない場合はリターン if (value == "") { return list; } //選択したセレクトボックスとlifeがマッチするかでフィルタリング return _.filter(list, function(item) { switch (value) { case '1': return item.life <= 10; case '2': return 10 < item.life && item.life <= 20; case '3': return 20 < item.life && item.life <= 30; case '4': return 30 < item.life && item.life <= 40; case '5': return 40 < item.life && item.life <= 50; case '6': return 50 < item.life; } }); } /*================================================================ 絞り込み[2] チェックボックス絞り込み関数 ================================================================*/ function filterByTag(list, value) { //絞り込み指定がない場合はリターン if (value.length == 0) { return list; } //選択した属性(チェックボックス)とtagがマッチするかでフィルタリング return _.filter(list, function(item) { var isMatch = false; //配列同士の比較 _.each(value, function(chkItem, i) { _.each(item.tag, function(tagItem, i) { if (tagItem === $(chkItem).val()) { isMatch = true; } }); }); return isMatch; }); } /*================================================================ 絞り込み[3] テキストボックス絞り込み関数 ================================================================*/ function filterByKeyword(list, value) { //絞り込み指定がない場合はリターン if (value == "") { return list; } //検索キーワードを配列に格納(スペースがある場合は複数格納) var freeAry = [];  var val = value.replace(/ /g, " "); searchAry = val.split(" "); //入力したキーワードがtitleもしくdescriptionにマッチするかでフィルタリング return _.filter(list, function(item) { var isMatch = false; _.each(searchAry, function(data, i) { if (item.title.indexOf(data) != -1 || item.description.indexOf(data) != -1) { isMatch = true; } }); return isMatch; }); } /*================================================================ スクリプトはじめ ================================================================*/ init(); });