3 回答

TA貢獻(xiàn)1946條經(jīng)驗 獲得超3個贊
正如其他人已經(jīng)說過的,您需要移動行創(chuàng)建
這是一個更簡單的版本
var data = [
{ ID: "1002", EMAIL: "hello@sample.com" },
{ ID: "1004", EMAIL: "hello2@sample.com"},
{ ID: "1006", EMAIL: "hello3@sample.com"},
{ ID: "1008", EMAIL: "hello4@sample.com"}
];
document.getElementById("excelTable").innerHTML = [
'<table border="1"><thead>',
...Object.keys(data[0]).map(key => `<th>${key}</th>`),
'</thead><tbody>',
...data.map(item => `<tr><td>${item.ID}</td><td>${item.EMAIL}</td></tr>`),
'</tbody></table>']
.join("")
<div id="excelTable"></div>

TA貢獻(xiàn)1825條經(jīng)驗 獲得超6個贊
問題是您在插入新單元格時創(chuàng)建新行,請嘗試我們創(chuàng)建一行的地方
var data = [{
ID: "1002",
EMAIL: "hello@sample.com"
},
{
ID: "1004",
EMAIL: "hello2@sample.com"
},
{
ID: "1006",
EMAIL: "hello3@sample.com"
},
{
ID: "1008",
EMAIL: "hello4@sample.com"
}
];
var table = document.createElement("table");
table.border = "1";
var cell = "";
var row = table.insertRow(-1);
//Add the header cells
var headerCell = document.createElement("TH");
headerCell.innerHTML = ("ID");
row.appendChild(headerCell);
headerCell = document.createElement("TH");
headerCell.innerHTML = ("EMAIL");
row.appendChild(headerCell);
data.forEach(function(obj) {
//Get an array of all available keys in current element
var keys = Object.keys(obj);
//Loop through all obtained keys
keys.forEach(function(key) {
//The following line will match ID/IDS/id/ids
if (key.toUpperCase().indexOf("ID") > -1) {
//Add the data cells
row = table.insertRow(-1);
cell = row.insertCell(-1);
cell.innerHTML = obj[key];
//console.log("found ids: ", obj[key]);
}
//The following line will match AMOUNT/AMOUNTS/amount/amounts
if (key.toUpperCase().indexOf("EMAIL") > -1) {
//Add the data cells
cell = row.insertCell(-1);
cell.innerHTML = obj[key];
//console.log("found emails: ", obj[key]);
}
});
});
var dvExcel = document.getElementById("excelTable");
dvExcel.innerHTML = "";
dvExcel.appendChild(table);
<div id="excelTable"></div>

TA貢獻(xiàn)1777條經(jīng)驗 獲得超10個贊
您正在為所有鍵創(chuàng)建行,將行移動到外循環(huán)中
var data = [{
ID: "1002",
EMAIL: "hello@sample.com"
},
{
ID: "1004",
EMAIL: "hello2@sample.com"
},
{
ID: "1006",
EMAIL: "hello3@sample.com"
},
{
ID: "1008",
EMAIL: "hello4@sample.com"
}
];
var table = document.createElement("table");
table.border = "1";
var cell = "";
var row = table.insertRow(-1);
//Add the header cells
var headerCell = document.createElement("TH");
headerCell.innerHTML = ("ID");
row.appendChild(headerCell);
headerCell = document.createElement("TH");
headerCell.innerHTML = ("EMAIL");
row.appendChild(headerCell);
data.forEach(function(obj) {
//Get an array of all available keys in current element
var keys = Object.keys(obj);
var row = table.insertRow(-1);
//Loop through all obtained keys
keys.forEach(function(key) {
//The following line will match ID/IDS/id/ids
if (key.toUpperCase().indexOf("ID") > -1) {
//Add the data cells
cell =row.insertCell(-1);
cell.innerHTML = obj[key];
//console.log("found ids: ", obj[key]);
}
//The following line will match AMOUNT/AMOUNTS/amount/amounts
if (key.toUpperCase().indexOf("EMAIL") > -1) {
//Add the data cells
cell = row.insertCell(-1);
cell.innerHTML = obj[key];
//console.log("found emails: ", obj[key]);
}
});
});
var dvExcel = document.getElementById("excelTable");
dvExcel.innerHTML = "";
dvExcel.appendChild(table);
<div id="excelTable"></div>
添加回答
舉報