$(function() { /*================================================================ 情報 ================================================================*/ var allList = [ { id: "1970", life: "148.8", url:"index.php?act=staff&id=1970", img:"user_data/11092.jpg", nen:"平成29年式", soukou:"54211", title:"【販3末】トヨタシエンタX白6750", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2008", life: "158.8", url:"index.php?act=staff&id=2008", img:"user_data/11175.jpg", nen:"平成27年式", soukou:"69300", title:"【ツ】トヨタヴォクシーX Cパッケージ銀6708", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2010", life: "178.8", url:"index.php?act=staff&id=2010", img:"user_data/11205.jpg", nen:"平成17年式", soukou:"112644", title:"【あ単】ハイエースグランドキャビン銀1196", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2045", life: "198.8", url:"index.php?act=staff&id=2045", img:"user_data/12964.jpg", nen:"令和2年式", soukou:"26200", title:"ライズZ白6867", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2056", life: "198.8", url:"index.php?act=staff&id=2056", img:"user_data/13137.jpg", nen:"令和1年式", soukou:"22664", title:"ライズ白G6790", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2057", life: "198.8", url:"index.php?act=staff&id=2057", img:"user_data/13134.jpg", nen:"令和1年式", soukou:"41173", title:"ライズZ青6896", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2063", life: "198.8", url:"index.php?act=staff&id=2063", img:"user_data/13138.jpg", nen:"令和2年式", soukou:"15112", title:"ロッキーG6914", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2124", life: "118.8", url:"index.php?act=staff&id=2124", img:"user_data/13617.jpg", nen:"平成21年式", soukou:"97061", title:"【あ単レ】エスティマ4WD ハイブリッドX白1310", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2315", life: "248.8", url:"index.php?act=staff&id=2315", img:"user_data/13565.jpg", nen:"令和4年式", soukou:"21195", title:"ライズZ白7459", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2336", life: "238.8", url:"index.php?act=staff&id=2336", img:"user_data/13567.jpg", nen:"令和4年式", soukou:"16269", title:"ライズZ白7363", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2457", life: "198.8", url:"index.php?act=staff&id=2457", img:"user_data/13753.jpg", nen:"令和2年式", soukou:"21772", title:"ライズG赤", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2479", life: "138.8", url:"index.php?act=staff&id=2479", img:"user_data/13825.jpg", nen:"平成28年式", soukou:"50408", title:"シエンタハイブリッドG黄色7702", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2480", life: "108.8", url:"index.php?act=staff&id=2480", img:"user_data/13838.jpg", nen:"平成28年式", soukou:"35302", title:"トヨタシエンタ銀G7704", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2482", life: "178.8", url:"index.php?act=staff&id=2482", img:"user_data/13814.jpg", nen:"平成29年式", soukou:"66859", title:"エクストレイル赤1659 20Xハイブリッド4WD", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2487", life: "118.8", url:"index.php?act=staff&id=2487", img:"user_data/13830.jpg", nen:"平成29年式", soukou:"44684", title:"トヨタシエンタ青X7715 Vパッケージ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2494", life: "138.8", url:"index.php?act=staff&id=2494", img:"user_data/14132.jpg", nen:"平成27年式", soukou:"78785", title:"エクストレイル銀4WD20X HV eブレーキパッケージ1673", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2525", life: "98.8", url:"index.php?act=staff&id=2525", img:"user_data/15479.jpg", nen:"平成26年式", soukou:"51811", title:"VWザ・ビートルデザイン白1728", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2526", life: "108.8", url:"index.php?act=staff&id=2526", img:"user_data/15486.jpg", nen:"平成27年式", soukou:"63848", title:"BMWミニクーパー赤1735", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2529", life: "98.8", url:"index.php?act=staff&id=2529", img:"user_data/15485.jpg", nen:"平成25年式", soukou:"66140", title:"VWザ・ビートルデザイン黒1729", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2530", life: "98.8", url:"index.php?act=staff&id=2530", img:"user_data/15483.jpg", nen:"平成26年式", soukou:"38861", title:"VWザ・ビートルデザイン黒", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2531", life: "108.8", url:"index.php?act=staff&id=2531", img:"user_data/15481.jpg", nen:"平成27年式", soukou:"53343", title:"VWザ・ビートルデザインLブルー1737", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2532", life: "108.8", url:"index.php?act=staff&id=2532", img:"user_data/15484.jpg", nen:"平成26年式", soukou:"55199", title:"BMWミニクーパーSオレンジ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2533", life: "68.8", url:"index.php?act=staff&id=2533", img:"user_data/15241.jpg", nen:"平成25年式", soukou:"57440", title:"BMWミニクーパー赤7887", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2534", life: "108.8", url:"index.php?act=staff&id=2534", img:"user_data/15480.jpg", nen:"平成28年式", soukou:"58787", title:"VWザ・ビートルベース赤1736", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "2537", life: "68.8", url:"index.php?act=staff&id=2537", img:"user_data/15563.jpg", nen:"平成26年式", soukou:"66316", title:"BMWミニクーパー赤", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2538", life: "68.8", url:"index.php?act=staff&id=2538", img:"user_data/15566.jpg", nen:"平成25年式", soukou:"54049", title:"BMWミニMINI赤", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2539", life: "198.8", url:"index.php?act=staff&id=2539", img:"user_data/15502.jpg", nen:"令和2年式", soukou:"39830", title:"トヨタライズG水", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2541", life: "198.8", url:"index.php?act=staff&id=2541", img:"user_data/15564.jpg", nen:"令和1年式", soukou:"19829", title:"ロッキーX銀7927", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2542", life: "88.8", url:"index.php?act=staff&id=2542", img:"user_data/15567.jpg", nen:"平成27年式", soukou:"62057", title:"BMWミニクーパー赤", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "2552", life: "108.8", url:"index.php?act=staff&id=2552", img:"user_data/15560.jpg", nen:"平成28年式", soukou:"52482", title:"VWザ・ビートルブルー", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "1247", life: "268.8", url:"index.php?act=staff&id=1247", img:"user_data/8680.jpg", nen:"平成29年式", soukou:"96653", title:"ハイエースワゴングランドキャビン10名乗り851", 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(); });