And*_*een 32 javascript html-table
在JavaScript中,是否可以从2D数组生成HTML表?编写HTML表的语法往往非常冗长,所以我想从2D JavaScript数组生成一个HTML表,如下所示:
[["row 1, cell 1", "row 1, cell 2"],
["row 2, cell 1", "row 2, cell 2"]]
Run Code Online (Sandbox Code Playgroud)
会成为:
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
所以我正在尝试编写一个JavaScript函数,它将从2D JavaScript数组返回一个表,如下所示:
function getTable(array){
//take a 2D JavaScript string array as input, and return an HTML table.
}
Run Code Online (Sandbox Code Playgroud)
bma*_*ity 48
这是一个将使用dom而不是字符串连接的函数.
function createTable(tableData) {
var table = document.createElement('table');
var tableBody = document.createElement('tbody');
tableData.forEach(function(rowData) {
var row = document.createElement('tr');
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
table.appendChild(tableBody);
document.body.appendChild(table);
}
createTable([["row 1, cell 1", "row 1, cell 2"], ["row 2, cell 1", "row 2, cell 2"]]);
Run Code Online (Sandbox Code Playgroud)
Dan*_*ams 27
这对于双循环来说非常容易.
function makeTableHTML(myArray) {
var result = "<table border=1>";
for(var i=0; i<myArray.length; i++) {
result += "<tr>";
for(var j=0; j<myArray[i].length; j++){
result += "<td>"+myArray[i][j]+"</td>";
}
result += "</tr>";
}
result += "</table>";
return result;
}
Run Code Online (Sandbox Code Playgroud)
这是使用模板文字的版本。它maps
根据模板文字创建新的字符串数组的数据,然后使用以下命令将它们添加到文档中insertAdjacentHTML
:
let data = [
['Title', 'Artist', 'Duration', 'Created'],
['hello', 'me', '2', '2019'],
['ola', 'me', '3', '2018'],
['Bob', 'them', '4.3', '2006']
];
function getCells(data, type) {
return data.map(cell => `<${type}>${cell}</${type}>`).join('');
}
function createBody(data) {
return data.map(row => `<tr>${getCells(row, 'td')}</tr>`).join('');
}
function createTable(data) {
// Destructure the headings (first row) from
// all the rows
const [headings, ...rows] = data;
// Return some HTML that uses `getCells` to create
// some headings, but also to create the rows
// in the tbody.
return `
<table>
<thead>${getCells(headings, 'th')}</thead>
<tbody>${createBody(rows)}</tbody>
</table>
`;
}
// Bang it altogether
document.body.insertAdjacentHTML('beforeend', createTable(data));
Run Code Online (Sandbox Code Playgroud)
table { border-collapse: collapse; }
tr { border: 1px solid #dfdfdf; }
th, td { padding: 2px 5px 2px 5px;}
Run Code Online (Sandbox Code Playgroud)
小智 5
另一个没有innerHTML 的版本。
function makeTable(array) {
var table = document.createElement('table');
for (var i = 0; i < array.length; i++) {
var row = document.createElement('tr');
for (var j = 0; j < array[i].length; j++) {
var cell = document.createElement('td');
cell.textContent = array[i][j];
row.appendChild(cell);
}
table.appendChild(row);
}
return table;
}
Run Code Online (Sandbox Code Playgroud)