Skip to content

Instantly share code, notes, and snippets.

@farishan
Created March 27, 2022 09:37
Show Gist options
  • Save farishan/7aec62f7ed50c152cac3bcea252275d1 to your computer and use it in GitHub Desktop.
Save farishan/7aec62f7ed50c152cac3bcea252275d1 to your computer and use it in GitHub Desktop.
define(() => {
function objectToTable(object = {}, header = {}, orientation = 'landscape') {
const $table = document.createElement('table')
let $thead
if (orientation === 'landscape') {
$thead = document.createElement('thead')
$table.appendChild($thead)
}
const $tbody = document.createElement('tbody')
$table.appendChild($tbody)
if (orientation === 'landscape') {
const $thRow = document.createElement('tr')
const $tdRow = document.createElement('tr')
for (let [k, v] of Object.entries(header)) {
const $th = document.createElement('th')
$th.innerHTML = v
$thRow.appendChild($th)
const $td = document.createElement('td')
$td.innerHTML = object[k]
$tdRow.appendChild($td)
}
$thead.appendChild($thRow)
$tbody.appendChild($tdRow)
} else if (orientation === 'potrait') {
for (let [k, v] of Object.entries(header)) {
const $tdRow = document.createElement('tr')
const $th = document.createElement('th')
$th.innerHTML = v
$tdRow.appendChild($th)
const $td = document.createElement('td')
$td.innerHTML = object[k]
$tdRow.appendChild($td)
$tbody.appendChild($tdRow)
}
}
return $table
}
return objectToTable
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment