Created
April 11, 2024 23:03
-
-
Save jrichardsz/ae9d42831a169187690b938ed6b2cd20 to your computer and use it in GitHub Desktop.
html table with javascript, array double to table html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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