1 回答
TA贡献1795条经验 获得超7个赞
使用包含要显示的属性的数组,而不是遍历所有列。
const myColumns = ["Carat", "Clarity", "Color"];
function generateTable(table, data, columns) {
for (let element of data) {
let row = table.insertRow();
for (key in columns) {
let cell = row.insertCell();
let text = document.createTextNode(element[key]);
cell.appendChild(text);
}
}
}
function generateTableHead(table, data) {
let thead = table.createTHead();
let row = thead.insertRow();
for (let key of data) {
let th = document.createElement("th");
let text = document.createTextNode(key);
th.appendChild(text);
row.appendChild(th);
}
}
generateTable(table, myData, myColumns);
generateTableHead(table, myColumns);
添加回答
举报