' + line.title + '
'; outputHtml += '価格 ' + line.life + '万円
'; outputHtml += '年式: ' + line.nen + '
'; outputHtml += '走行: 約 ' + line.soukou + 'km
'; outputHtml += '$(function() {
/*================================================================
情報
================================================================*/
var allList = [
{
id: "1814",
life: "54.8",
url:"index.php?act=staff&id=1814",
img:"user_data/10898.jpg",
nen:"平成26年式",
soukou:"87,000*",
title:"デイズハイウェイスター2876",
sold:"",
new:"",
tag: ["","販売中","","AT",""],
description: "tesut"
},
{
id: "1926",
life: "134.8",
url:"index.php?act=staff&id=1926",
img:"user_data/11016.jpg",
nen:"令和2年式",
soukou:"62,000",
title:"スペーシアHV X",
sold:"",
new:"",
tag: ["","販売中","","AT",""],
description: "tesut"
},
{
id: "1938",
life: "144.8",
url:"index.php?act=staff&id=1938",
img:"user_data/11202.jpg",
nen:"平成30年式",
soukou:"53090",
title:"スペーシアG HV3067",
sold:"",
new:"",
tag: ["","販売中","","AT",""],
description: "tesut"
},
{
id: "1940",
life: "158.8",
url:"index.php?act=staff&id=1940",
img:"user_data/11184.jpg",
nen:"令和3年式",
soukou:"11465",
title:"【ツ単楽】ニッサンルークスX赤3066",
sold:"",
new:"",
tag: ["","販売中","","AT",""],
description: "tesut"
},
{
id: "1974",
life: "144.8",
url:"index.php?act=staff&id=1974",
img:"user_data/11201.jpg",
nen:"平成29年式",
soukou:"25,140",
title:"スペーシアG HV3134",
sold:"",
new:"",
tag: ["","販売中","","AT",""],
description: "tesut"
},
{
id: "1979",
life: "134.8",
url:"index.php?act=staff&id=1979",
img:"user_data/11209.jpg",
nen:"令和2年式",
soukou:"28991",
title:"スペーシアG HV3081",
sold:"",
new:"",
tag: ["","販売中","","AT",""],
description: "tesut"
},
{
id: "2174",
life: "174.8",
url:"index.php?act=staff&id=2174",
img:"user_data/13446.jpg",
nen:"令和4年式",
soukou:"6849",
title:"スペーシアG HV3360",
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.life + '万円 年式: ' + line.nen + ' 走行: 約 ' + line.soukou + 'km' + line.title + '
';
outputHtml += '';
outputHtml += '' + line.new + '' + line.sold + '';
outputHtml += '
条件に当てはまる車を検索できませんでした。
' + 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(); });