$(function() { /*================================================================ 情報 ================================================================*/ var allList = [ { id: "1086", life: "226.8", url:"index.php?act=staff&id=1086", img:"user_data/7576.jpg", nen:"平成15年式", soukou:"168450", title:"コースター観音開き金トヨタ仕入→栃木", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "1082", life: "49.8", url:"index.php?act=staff&id=1082", img:"user_data/7547.jpg", nen:"平成26年式", soukou:"69556", title:"ADバンVE白 ニッサン 仕入先→福岡", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "1080", life: "69.8", url:"index.php?act=staff&id=1080", img:"user_data/7541.jpg", nen:"平成14年式", soukou:"144476", title:"サンバートラックダンプ4WD白スバル仕入先→愛知", sold:"", new:"", tag: ["新着","販売中","MT","",""], description: "tesut" }, { id: "1079", life: "59.8", url:"index.php?act=staff&id=1079", img:"user_data/7543.jpg", nen:"平成20年式", soukou:"79634", title:"サンバートラック4WD7688 TB白スバル仕入先→愛知", sold:"", new:"", tag: ["新着","","MT","","売り切れ"], description: "tesut" }, { id: "1076", life: "54.8", url:"index.php?act=staff&id=1076", img:"user_data/7545.jpg", nen:"平成18年式", soukou:"36712", title:"サンバートラック408 TB白仕入先→大阪", sold:"", new:"", tag: ["新着","販売中","MT","",""], description: "tesut" }, { id: "1075", life: "49.8", url:"index.php?act=staff&id=1075", img:"user_data/7544.jpg", nen:"平成17年式", soukou:"57622", title:"サンバートラック4WD TB白仕入先→福岡", sold:"", new:"", tag: ["新着","販売中","MT","",""], description: "tesut" }, { id: "1067", life: "64.8", url:"index.php?act=staff&id=1067", img:"user_data/7497.jpg", nen:"平成20年式", soukou:"98591", title:"タウンエースバンGL銀 トヨタ 仕入先→埼玉", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "1050", life: "59.8", url:"index.php?act=staff&id=1050", img:"user_data/7002.jpg", nen:"平成20年式", soukou:"106111", title:"ライトエースバンGL銀 トヨタ 仕入先→大阪", sold:"", new:"", tag: ["新着","","","AT","売り切れ"], description: "tesut" }, { id: "1035", life: "59.8", url:"index.php?act=staff&id=1035", img:"user_data/6944.jpg", nen:"平成27年式", soukou:"58213", title:"㊻ADバン VEホワイト仕入先:沖縄", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "1031", life: "59.8", url:"index.php?act=staff&id=1031", img:"user_data/6952.jpg", nen:"平成25年式", soukou:"94266", title:"㊷ハイゼットバン ハイルーフ白仕入先:沖縄", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "1030", life: "49.8", url:"index.php?act=staff&id=1030", img:"user_data/6951.jpg", nen:"平成19年式", soukou:"93000", title:"㊶キャリートラック4WD ホワイト仕入先:沖縄", sold:"", new:"", tag: ["","","MT","","売り切れ"], description: "tesut" }, { id: "989", life: "58.8", url:"index.php?act=staff&id=989", img:"user_data/6933.jpg", nen:"平成25年式", soukou:"96000", title:"ハイゼットトラックスペシャル4WD白395 仕入先→岐阜", sold:"", new:"", tag: ["新着","販売中","MT","",""], description: "tesut" }, { id: "988", life: "39.8", url:"index.php?act=staff&id=988", img:"user_data/6955.jpg", nen:"平成20年式", soukou:"96000", title:"サンバートラックPRO4WD 白仕入先→岐阜", sold:"", new:"", tag: ["新着","販売中","MT","",""], description: "tesut" }, { id: "985", life: "56.8", url:"index.php?act=staff&id=985", img:"user_data/6941.jpg", nen:"平成26年式", soukou:"86127", title:"ミニキャブバン青 ミツビシ 仕入先→神奈川", sold:"", new:"", tag: ["新着","","","AT","売り切れ"], description: "tesut" }, { id: "982", life: "36.8", url:"index.php?act=staff&id=982", img:"user_data/6894.jpg", nen:"平成21年式", soukou:"61509", title:"クリッパーバン白367ニッサン 仕入先→沖縄", sold:"", new:"", tag: ["","販売中","MT","",""], description: "tesut" }, { id: "981", life: "58.8", url:"index.php?act=staff&id=981", img:"user_data/6903.jpg", nen:"平成25年式", soukou:"117293", title:"スクラムワゴン銀1855 マツダ 仕入先→福岡", sold:"", new:"", tag: ["新着","","","AT","売り切れ"], description: "tesut" }, { id: "980", life: "69.8", url:"index.php?act=staff&id=980", img:"user_data/6902.jpg", nen:"平成26年式", soukou:"92861", title:"ハイゼットトラック白 ダイハツ 仕入先→神戸", sold:"", new:"", tag: ["新着","","","AT","売り切れ"], description: "tesut" }, { id: "899", life: "49.8", url:"index.php?act=staff&id=899", img:"user_data/6378.jpg", nen:"平成26年式", soukou:"92301", title:"ハイゼットカーゴ白386 ダイハツ 仕入先→沖縄", sold:"", new:"", tag: ["","","","AT","売り切れ"], description: "tesut" }, { id: "687", life: "158.8", 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: "681", life: "38.8", url:"index.php?act=staff&id=681", img:"user_data/4718.jpg", nen:"平成23年式", soukou:"64500", title:"プロボックス銀190 トヨタ", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "663", life: "36.8", url:"index.php?act=staff&id=663", img:"user_data/4479.jpg", nen:"平成23年式", soukou:"76100", title:"プロボックス白183 トヨタ", sold:"", new:"", tag: ["","販売中","","AT",""], description: "tesut" }, { id: "662", life: "42.8", url:"index.php?act=staff&id=662", img:"user_data/4478.jpg", nen:"平成21年式", soukou:"38113", title:"サンバーT銀338 スバル", sold:"", new:"", tag: ["","販売中","MT","",""], description: "tesut" }, { id: "648", life: "49.8", url:"index.php?act=staff&id=648", img:"user_data/4435.jpg", nen:"平成25年式", soukou:"122200", title:"エブリーバン白329 スズキ", sold:"", new:"", tag: ["新着","","","AT","売り切れ"], description: "tesut" }, { id: "629", life: "24.8", url:"index.php?act=staff&id=629", img:"user_data/4398.jpg", nen:"平成17年式", soukou:"123355", title:"クリッパーバン白333ニッサン", sold:"", new:"", tag: ["新着","","MT","","売り切れ"], description: "tesut" }, { id: "595", life: "44.8", url:"index.php?act=staff&id=595", img:"user_data/3951.jpg", nen:"平成25年式", soukou:"115152", title:"ハイゼット白303 ダイハツ", sold:"", new:"", tag: ["新着","販売中","MT","",""], description: "tesut" }, { id: "398", life: "23.8", url:"index.php?act=staff&id=398", img:"user_data/2164.jpg", nen:"平成20年式", soukou:"80648", title:"AD白157 ニッサン", sold:"", new:"", tag: ["","","","AT","売り切れ"], description: "tesut" }, { id: "392", life: "19.8", url:"index.php?act=staff&id=392", img:"user_data/2271.jpg", nen:"平成17年式", soukou:"130256", title:"サンバートラック白284スバル", sold:"", new:"", tag: ["新着","","MT","","売り切れ"], description: "tesut" }, { id: "1113", life: "", url:"index.php?act=staff&id=1113", img:"./img/nophoto.png", nen:"平成05年式", soukou:"120000", title:"クイックデリバリー4WD 白 トヨタ 仕入先→福岡", sold:"", new:"", tag: ["新着","販売中","","AT",""], description: "tesut" }, { id: "1111", life: "", url:"index.php?act=staff&id=1111", img:"./img/nophoto.png", nen:"平成23年式", soukou:"78000", title:"アトレーワゴン スローパー ダイハツ 白 仕入先→千葉", 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(); });