Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 21, 2023 13:17
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 code-boxx/ea6b63369c717c0a88b700fa3d60f6e3 to your computer and use it in GitHub Desktop.
Save code-boxx/ea6b63369c717c0a88b700fa3d60f6e3 to your computer and use it in GitHub Desktop.
Javascript Timetable

JAVASCRIPT TIMETABLE

https://code-boxx.com/simple-javascript-timetable/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) WHOLE PAGE */
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
/* (B) TIMETABLE WRAPPER */
.timetable {
display: grid;
position: relative;
}
/* (C) BLANK & HEADER CELLS */
.cell {
display: flex;
align-items: center;
padding: 10px;
border: 1px solid #efefef;
}
.head {
font-weight: 700;
justify-content: center;
color: #333;
background: #f9f9f9;
}
/* (D) ENTRY CELLS */
.entry {
position: absolute;
right: 0; left: 0; top: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<!DOCTYPE html>
<html>
<head>
<title>Time Table With CSS Grid</title>
<!-- (A) LOAD CSS & JS -->
<link rel="stylesheet" href="js-timetable.css">
<script src="js-timetable.js"></script>
</head>
<body>
<!-- (B) GENERATE TIMETABLE HERE -->
<div id="demo"></div>
<!-- (C) ATTACH TIMETABLE -->
<script>
timetable({
// (C1) REQUIRED
target: document.getElementById("demo"),
x: ["Morning", "Afternoon", "Night"],
y: ["Mon", "Tue", "Wed", "Thur", "Fri"],
data: [
{
txt: "Feed the Doge",
row: "2", col: "2/3",
color: "white", bg: "black",
click : () => { alert("Clicked!"); }
},
{
txt: "Walk the Doge",
row: "4", col: "3/4",
color: "#04ff00", bg: "#090099"
},
{
txt: "Play with Doge",
row: "6", col: "3",
color: "#fff", bg: "#aa18ad"
}
],
// (C2) OPTIONAL
gridX: "100px repeat(3, 1fr)",
gridY : "50px"
});
</script>
</body>
</html>
function timetable (instance) {
// (A) CSS
instance.target.classList.add("timetable");
if (instance.gridX == undefined) {
instance.gridX = `repeat(${instance.x.length+1}, 1fr)`;
}
instance.target.style.gridTemplateColumns = instance.gridX;
if (instance.gridY) {
instance.target.style.gridAutoRows = instance.gridY;
}
// (B) GENERATE CELLS HELPER FUNCTION
let celler = (data, css) => {
let cell = document.createElement("div");
cell.className = css;
if (typeof data == "string") { cell.innerHTML = data; }
else {
cell.innerHTML = data.txt;
cell.style = `grid-column:${data.col};grid-row:${data.row};color:${data.color};background:${data.bg}`;
if (instance.gridY) { cell.style.height = instance.gridY; }
if (data.click) { cell.onclick = data.click; }
}
instance.target.appendChild(cell);
};
// (C) FIRST ROW - EMPTY CELL | HEADER FOR X
celler("&nbsp;", "cell head");
for (let i of instance.x) { celler(i, "cell head"); }
// (D) FOLLOWING ROWS - HEADER FOR Y | EMPTY CELLS
for (let i=0; i<instance.y.length; i++) {
celler(instance.y[i], "cell head");
for (let j=0; j<instance.x.length; j++) { celler("&nbsp;", "cell"); }
}
// (E) ENTRIES
for (let i of instance.data) { celler(i, "cell entry"); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment