2017-12-08 6 views
0

私は異なる属性を持つ車の配列を持っています。私はボタンを押すとミニバンだけを表示する機能を作りようとしています。ボタンを押したときに何も表示されません。私は完全なプログラムを実行するとエラーは発生しません。私が間違っていることについてのヒントは非常に役に立ちます!配列から特定のプロパティを表示

// Define car object using a constructor function 
function Car(id, car_make, car_model, car_year, car_type, car_color, 
car_price, car_mileage) { 
    this.stockid = id; 
    this.make = car_make; 
    this.model = car_model; 
    this.year = car_year; 
    this.type = car_type; 
    this.color = car_color; 
    this.price = car_price; 
    this.mileage = car_mileage; 
} 


// Declare an array of Car objects 
var car_list = []; 

// Create an instance of the Car object and add it to the car_list array 
    car_list.push(new Car(1001, "Toyota", "Camry", 2011, "Sedan", "Gray", 
     17555, 55060)); 
    car_list.push(new Car(1002, "Volvo", "s40", 2013, "Sedan", "Black", 
     15575, 20350)); 
    car_list.push(new Car(1251, "Toyota", "Sienna", 2008, "Minivan", "Gray", 
     15775, 70000)); 
    car_list.push(new Car(1321, "Porsche", "Panamera", 2012, "Hatchback", 
     "Red", 104250, 10567)); 
    car_list.push(new Car(1904, "Honda", "Accord", 2009, "Sedan", "White", 
     13370, 35000)); 
    car_list.push(new Car(1855, "Toyota", "Highlander", 2008, "SUV", 
     "Silver", 18555, 55060)); 
    car_list.push(new Car(1543, "Ford", "Fusion", 2011, "Sedan", "Black", 
     13575, 90350)); 
    car_list.push(new Car(1345, "Toyota", "Sienna", 2011, "Minivan", "Gray", 
     25775, 70000)); 
    car_list.push(new Car(2133, "Dodge", "Caravan", 2012, "Minivan", "Red", 
     30250, 17567)); 
    car_list.push(new Car(2999, "Lexus", "LFA", 2012, "coupe", "Red", 381370, 
     3500)); 
    car_list.push(new Car(3001, "Ferrari", "Rubino", 2012, "coupe", "Red", 
     354370, 5500)); 
    car_list.push(new Car(4002, "Audi", "R8", 2012, "convertible", "Black", 
     181370, 4500)); 
// Display car list 

for (var i=0; i<car_list.length; i++){ 
    var this_element = "<tr class='car-item' id='l-"+i+"' >" ; 
    this_element += "<td>" + car_list[i].stockid + "</td><td>" + 
car_list[i].make + "</td>"; 
    this_element += "<td>" + car_list[i].model + "</td><td>" + 
car_list[i].year + "</td><td>" + car_list[i].type + "</td>"; 
    this_element += "<td>" + car_list[i].color + "</td><td> $" + 
car_list[i].price + "</td>"; 
    this_element += "<td>" + car_list[i].mileage + "</td>"; 
    this_element += "<td><button type='button' data-index='"+i+"' 
class='addme btn btn-primary' "; 
    this_element += "data-stockid='"+ car_list[i].stockid + "'>Add</button> 
</td></tr>"; 

    $("#car-list").append(this_element); 
} 

//display minivans 
$('#p3').on('click', function(){ 

    if(type = "Minivan"){ 

    var thisButton = $(this); 
    var index = thisButton.data('index'); 
    var thisCar = car_list[index]; 

    var newRow = "<tr><td>"+ thisCar.stockid + "</td><td>" + thisCar.make + " 
    </td><td>" + thisCar.model + "</td><td>" + thisCar.year + 
    "</td><td>" + thisCar.type + "</td><td>" + thisCar.color + "</td><td>" + 
    thisCar.price + "</td><td>" + thisCar.mileage + "</td></tr>"; 

    $('#minivan-list').append(newRow); 
} 

}); 
+0

にすべてのミニバンを追加したい場合はあなたのクリックハンドラであなたの車のリストをループしていません。 –

+0

'type'がどこにも定義されていないので、ブラウザコンソールでエラーが表示されます – charlietfl

+0

HTMLコードも入力できますか? – klugjo

答えて

1

あなたがタイプ「ミニバン」のリストを取得したい場合は、あなたのcar_list、アレイを通してループする必要があります。

for (var key in car_list) { 
    if (car_list[ key ].type == "Minivan") console.log(car_list[ key ]); 
} 

あなたは#minivan-list

var html = "" 

for (var key in car_list) { 
    if (car_list[ key ].type == "Minivan") { 
     html += "<tr><td>"+ car_list[ key ].stockid + "</td><td>" + car_list[ key ].make + "</td><td>" + car_list[ key ].model + "</td><td>" + car_list[ key ].year + "</td><td>" + car_list[ key ].type + "</td><td>" + car_list[ key ].color + "</td><td>" + car_list[ key ].price + "</td><td>" + car_list[ key ].mileage + "</td></tr>"; 
    } 
} 

$('#minivan-list').append(html); 
+0

本当にありがとうございました! –

+0

これはあなたが探しているものですか? – Eddie

+0

はい、私はそれらの変更を行い、それは今完璧に動作します! –

関連する問題