我正試圖用數據動態填充表格單元格。我跟着這個JSfiddle example。JavaScript將數據動態地填充到HTML表格中
這是我的代碼:
var heading = new Array();
heading[0] = "Type"
heading[1] = "Item Name"
heading[2] = "Brand"
heading[3] = "Unit Price"
heading[4] = "Quantity"
var stock = new Array();
for(var i = 0; i < topProductList.length; i++){
stock[0].push(topProductList[i].type);
stock[1].push(topProductList[i].itemName);
stock[2].push(topProductList[i].brand);
stock[3].push(topProductList[i].unitprice);
stock[4].push(topProductList[i].quantity);
console.log(topProductList[i].type + ' ' + topProductList[i].itemName + ' ' + topProductList[i].brand + ' ' + topProductList[i].unitprice + ' ' + topProductList[i].quantity);
}
我試圖通過我的數組循環添加的數據對於每一列。但是,我得到Cannot read property 'push' of undefined
。我打印出console.log
來檢查我是否正確檢索並且數據檢索沒有問題。只是當我試圖將每個數據添加到列中的部分遇到問題時。
任何想法?
用於topProductList樣本數據:
var topProductList = [{type: 'home appliance', name: 'Sound Teoh Tv Cable 5m Hl3599 Soundteoh', unitprice: 9.90, quantity: 5},
{type: 'kitchen appliance', name: ' Powerpac Mini Rice Cooker Pprc09 Powerpac', unitprice: 19.90, quantity: 5},
{type: 'kitchen appliance', name: ' Sona 2-slice Bread Toaster Sto2201 Sona', unitprice: 39.90, quantity: 5},
{type: 'home appliance', name: ' Simply Living. 10" Wall Clock Simply Living', unitprice: 9.90, quantity: 5},
{type: 'kitchen appliance', name: ' Morries Pie Maker Ms-8028pm Morries', unitprice: 29.90, quantity: 4}];
的樣本數據是無效的JSON因爲你沒有關閉的名字關鍵單引號,我已經更新了JSON ...看看。 – Shiladitya