0
我有以下幾點:的js只返回表的第一行
function builTable(){
for(i = 0; i< source.Products.length; i++){
var sourceProducts = source.Products[i];
row = '<tr><td>sth</td><td>';
row += sourceProducts.Product;
row += '</td><td>';
row += sourceProducts.Bookings + '</td><td>';
row += sourceProducts.Percentage + '%' + '</td><td>';
row += sourceProducts.Transactions + '</td></tr>';
$('table').append(row);
}
}
$(document).ready(function(){
builTable();
});
它:
function builTable(){
for(i = 0; i< source.Products.length; i++){
var sourceProducts = source.Products[i];
row = '<tr><td>sth</td><td>';
row += sourceProducts.Product;
row += '</td><td>';
row += sourceProducts.Bookings + '</td><td>';
row += sourceProducts.Percentage + '%' + '</td><td>';
row += sourceProducts.Transactions + '</td></tr>';
return row;
}
}
function generateTable(){
$('table').append(builTable());
}
$(document).ready(function(){
generateTable();
});
,如果我將其更改爲以下這僅附加表的第一行然而,工作正常,我想有一個更具結構化的代碼,其功能服務於他們的目的:
- 一個函數用於構建表和一個用於appendi (如第一個例子所示)。
JSON格式:
var source = {
"Products": [
{
"Product": "xxxxx.com",
"Bookings": 560,
"Percentage": 55.82,
"Transactions": "xxxx.xx"
},
{
"Product": "xxxxx Mobile",
"Bookings": 487,
"Percentage": 9.12,
"Transactions": "xxxx.xx"
},
{
"Product": "xx 24-7",
"Bookings": 478,
"Percentage": 8.95,
"Transactions": "xxxx.xx"
},
{
"Product": "xxxxxx",
"Bookings": 422,
"Percentage": 7.9,
"Transactions": "xxxx.xx"
},
{
"Product": "API",
"Bookings": 315,
"Percentage": 5.9,
"Transactions": "xxxx.xx"
},
{
"Product": "API",
"Bookings": 315,
"Percentage": 2.83,
"Transactions": "xxxx.xx"
}
],
"Total": {
"Bookings": 315,
"Transactions": "xxxx.xx"
}
}
任何幫助嗎?
打敗我吧;) – Cameron