Skip to content

Instantly share code, notes, and snippets.

@jrichardsz
Created April 11, 2024 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrichardsz/ae9d42831a169187690b938ed6b2cd20 to your computer and use it in GitHub Desktop.
Save jrichardsz/ae9d42831a169187690b938ed6b2cd20 to your computer and use it in GitHub Desktop.
html table with javascript, array double to table html
function Helper() {
function getCells(data, type) {
return data.map(cell => `<${type} style="border: 1px solid #000;" >${cell}</${type}>`).join('');
}
function createBody(data) {
return data.map(row => `<tr style="border: 1px solid #000;" >${getCells(row, 'td')}</tr>`).join('');
}
this.generateTable = (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 style="width:100%; border: 1px solid black;border-collapse: collapse;text-align: left;" >
<thead>${getCells(headings, 'th')}</thead>
<tbody>${createBody(rows)}</tbody>
</table>
`;
}
}
module.exports = Helper;
const Helper = require("./common/Helper.js")
var helper = new Helper();
var mailInbox = [];
//header
mailInbox.push(["to","subject","content","isHtml"])
//rows
mailInbox.push(["jane","message 1","hi 1","true"])
mailInbox.push(["kurt","message 2","hi 2","false"])
mailInbox.push(["rich","message 3","hi 3","true"])
//get html as string
helper.generateTable(mailInbox)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment