Last active
August 19, 2021 19:55
-
-
Save MWarCZ/0e321a4ad6fc49043bf869a07b79ec14 to your computer and use it in GitHub Desktop.
Responsive table
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>ResTable</title> | |
<script src="ResTable.js"></script> | |
</head> | |
<body> | |
<table class="res-table my-table"> | |
<tr> | |
<th>Jméno</th> | |
<th>Pozice</th> | |
<th>Telefon</th> | |
<th>Mobil</th> | |
<th>Email</th> | |
</tr> | |
<tr> | |
<td>xxx xxx</td> | |
<td>aaa</td> | |
<td>123 456 789</td> | |
<td>123 456 789</td> | |
<td>[email protected]</td> | |
</tr> | |
<tr> | |
<td>xxx xxx</td> | |
<td>aaa</td> | |
<td>123 456 789</td> | |
<td>123 456 789</td> | |
<td>[email protected]</td> | |
</tr> | |
</table> | |
<script> | |
window.addEventListener('load', function () { | |
var rt = new ResTable('.res-table', {breakpoint: '700px'}) | |
}) | |
</script> | |
<style> | |
@media screen and (max-width: 700px) { | |
.my-table tr { | |
padding: 0 .5rem; | |
border: 1px solid; | |
margin: 0.5rem; | |
} | |
.my-table td { line-height: 1.5em; } | |
.my-table td+td { border-top: 1px solid; } | |
} | |
</style> | |
</body> | |
</html> |
This file contains hidden or 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 ResTable(tableSelector, options = {}) { | |
var opts = Object.assign({ | |
breakpoint: '600px', | |
separator: ': ', | |
}, options) | |
this.tableSelector = tableSelector | |
this.style = document.createElement('style') | |
document.head.append(this.style) | |
this.setStyle(opts.breakpoint, opts.separator) | |
this.table = document.querySelector(tableSelector) | |
this.setupTable() | |
} | |
ResTable.prototype.setStyle = function (breakpoint = '600px', separator = ': ') { | |
var tableSelector = this.tableSelector | |
var style = ` | |
@media screen and (max-width: ${breakpoint}) { | |
${tableSelector} tr {display:block;} | |
${tableSelector} tr:first-child {display:none;} | |
${tableSelector} td {display:block;} | |
${tableSelector} td::before {content:attr(data-label) '${separator}';font-weight:bold;} | |
} | |
` | |
this.style.innerHTML = style | |
} | |
ResTable.prototype.setupTable = function () { | |
try { | |
var table = this.table | |
var labels = Array.from(table.querySelectorAll('th')).map(function (th) { return th.innerText }) | |
var tds = table.querySelectorAll('td') | |
for (var i = 0; i < tds.length; i++) { | |
if (typeof tds[i].dataset.label === 'undefined') { | |
tds[i].dataset.label = labels[i % labels.length] | |
} | |
} | |
} catch (e) { console.error(e) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment