$(function() { /*================================================================ 情報 ================================================================*/ var allList = [ { id: "884", life: "99", url:"index.php?act=staff&id=884", img:"user_data/6318.jpg", nen:"平成24年式", soukou:"84957", title:"セレナ黒 ニッサン", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "883", life: "99", url:"index.php?act=staff&id=883", img:"user_data/6317.jpg", nen:"平成24年式", soukou:"93415", title:"セレナ茶 ニッサン", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "882", life: "108", url:"index.php?act=staff&id=882", img:"user_data/6316.jpg", nen:"平成24年式", soukou:"92247", title:"セレナHV黒 ニッサン", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "881", life: "108", url:"index.php?act=staff&id=881", img:"user_data/6315.jpg", nen:"平成24年式", soukou:"87638", title:"セレナHVグレー ニッサン", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "873", life: "86", url:"index.php?act=staff&id=873", img:"user_data/6253.jpg", nen:"平成25年式", soukou:"105000", title:"セレナHV水色5206 ニッサン", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "832", life: "59", url:"index.php?act=staff&id=832", img:"user_data/5781.jpg", nen:"平成19年式", soukou:"38000", title:"ベンツA170白673", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "831", life: "53", url:"index.php?act=staff&id=831", img:"user_data/5780.jpg", nen:"平成19年式", soukou:"73000", title:"ベンツA170白672", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "817", life: "59", url:"index.php?act=staff&id=817", img:"user_data/5660.jpg", nen:"平成22年式", soukou:"65101", title:"BMW白669 BMW", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "777", life: "68", url:"index.php?act=staff&id=777", img:"user_data/4931.jpg", nen:"平成19年式", soukou:"83475", title:"ベンツB170白644", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "759", life: "216", url:"index.php?act=staff&id=759", img:"user_data/4908.jpg", nen:"平成17年式", soukou:"103185", title:"ハイエースW/GC銀6702", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "739", life: "69", url:"index.php?act=staff&id=739", img:"user_data/4895.jpg", nen:"平成20年式", soukou:"115700", title:"デリカ銀620 ミツビシ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "737", life: "55", url:"index.php?act=staff&id=737", img:"user_data/4893.jpg", nen:"平成17年式", soukou:"101728", title:"エスティマ白627 トヨタ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "726", life: "128", url:"index.php?act=staff&id=726", img:"user_data/4880.jpg", nen:"平成22年式", soukou:"56999", title:"ベンツCクラスW黒623", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "724", life: "62", url:"index.php?act=staff&id=724", img:"user_data/4884.jpg", nen:"平成19年式", soukou:"81505", title:"ベンツB170黒626", sold:"", new:"", tag: ["新着","","","AT","売り切れ"], description: "tesut" }, { id: "712", life: "59", url:"index.php?act=staff&id=712", img:"user_data/4834.jpg", nen:"平成19年式", soukou:"37062", title:"ベンツA170赤590メルセデス", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "703", life: "66", url:"index.php?act=staff&id=703", img:"user_data/4801.jpg", nen:"平成19年式", soukou:"81582", title:"デリカ白599 ミツビシ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "701", life: "126", url:"index.php?act=staff&id=701", img:"user_data/4808.jpg", nen:"平成20年式", soukou:"92200", title:"BMWカブリオレ黒582 BMW", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "700", life: "59", url:"index.php?act=staff&id=700", img:"user_data/4807.jpg", nen:"平成20年式", soukou:"80342", title:"BMW白584 BMW", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "687", life: "167", url:"index.php?act=staff&id=687", img:"user_data/4728.jpg", nen:"平成20年式", soukou:"48627", title:"ハイエースWDX白6507トヨタ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "672", life: "49", url:"index.php?act=staff&id=672", img:"user_data/4688.jpg", nen:"平成19年式", soukou:"60616", title:"BMW銀569 BMW", sold:"", new:"", tag: ["新着","","","AT","売り切れ"], description: "tesut" }, { id: "671", life: "128", url:"index.php?act=staff&id=671", img:"user_data/4630.jpg", nen:"平成21年式", soukou:"119500", title:"マスタング黒568 フォード", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "652", life: "98", url:"index.php?act=staff&id=652", img:"user_data/4432.jpg", nen:"平成21年式", soukou:"31863", title:"ミニ・クラブマン茶4768BMW", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "612", life: "68", url:"index.php?act=staff&id=612", img:"user_data/4170.jpg", nen:"平成20年式", soukou:"118141", title:"ヴォクシー白4663 トヨタ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "542", life: "48", url:"index.php?act=staff&id=542", img:"user_data/3486.jpg", nen:"平成24年式", soukou:"73000", title:"プントエヴォ4563フィアット", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "534", life: "69", url:"index.php?act=staff&id=534", img:"user_data/3461.jpg", nen:"平成18年式", soukou:"50735", title:"BMW銀511 BMW", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "515", life: "59", url:"index.php?act=staff&id=515", img:"user_data/3460.jpg", nen:"平成20年式", soukou:"97297", title:"ノア銀4414 トヨタ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "327", life: "ご予算♪", url:"index.php?act=staff&id=327", img:"user_data/750.jpg", nen:"ご希望に応じて♪", soukou:"ご希望による♪", title:"リクエストプラン!!", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "304", life: "19", url:"index.php?act=staff&id=304", img:"user_data/205.jpg", nen:"平成19年式", soukou:"142937", title:"ランディ銀4796 スズキ", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "164", life: "39", url:"index.php?act=staff&id=164", img:"user_data/1738.jpg", nen:"平成20年式", soukou:"118300", title:"セレナガンメタ3821日産", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "90", life: "118", url:"index.php?act=staff&id=90", img:"user_data/947.jpg", nen:"平成20年式", soukou:"75601", title:"キャラバン53黒ニッサン", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "89", life: "98", url:"index.php?act=staff&id=89", img:"user_data/1950.jpg", nen:"平成18年式", soukou:"171500", title:"キャラバン金5787 ニッサン", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "88", life: "88", url:"index.php?act=staff&id=88", img:"user_data/1348.jpg", nen:"平成19年式", soukou:"138422", title:"エリシオン黒330 ホンダ", 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(); });