Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active July 12, 2023 13:57
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/b250d1be28083bc95120c9560ddf7729 to your computer and use it in GitHub Desktop.
Save code-boxx/b250d1be28083bc95120c9560ddf7729 to your computer and use it in GitHub Desktop.
HTML Editable Table

HTML EDITABLE TABLE

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) ENTIRE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
/* (B) EDITABLE TABLE */
.editable {
border-collapse: collapse;
}
.editable th, .editable td {
text-align: left;
padding: 15px;
}
.editable thead {
color: #fff;
background: #8363fd;
}
.editable tbody tr:nth-child(even) {
background: #f2f2f2;
}
.editable td.edit {
background: #f8ff88;
}
<!DOCTYPE html>
<html>
<head>
<title>Editable Table</title>
<link rel="stylesheet" href="edit-table.css">
<script defer src="edit-table.js"></script>
</head>
<body>
<table class="editable">
<!-- (A) HEADER -->
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<!-- (B) DATA -->
<tbody>
<tr>
<td>Job Doe</td>
<td>job@doe.com</td>
</tr>
<tr>
<td>Joe Doe</td>
<td>joe@doe.com</td>
</tr>
<tr>
<td>Joi Doe</td>
<td>joi@doe.com</td>
</tr>
<tr>
<td>Jon Doe</td>
<td>jon@doe.com</td>
</tr>
<tr>
<td>Joy Doe</td>
<td>joy@doe.com</td>
</tr>
</tbody>
</table>
<div>Double-click on a cell to edit.</div>
<div>Press enter or click outside cell to commit.</div>
<div>Press escape to cancel.</div>
</body>
</html>
// (A) INITIALIZE - DOUBLE CLICK TO EDIT CELL
window.addEventListener("DOMContentLoaded", () => {
for (let cell of document.querySelectorAll(".editable td")) {
cell.ondblclick = () => editable.edit(cell);
}
});
var editable = {
// (B) PROPERTIES
selected : null, // current selected cell
value : "", // current selected cell value
// (C) "CONVERT" TO EDITABLE CELL
edit : cell => {
// (C1) REMOVE "DOUBLE CLICK TO EDIT"
cell.ondblclick = "";
// (C2) EDITABLE CONTENT
cell.contentEditable = true;
cell.focus();
// (C3) "MARK" CURRENT SELECTED CELL
cell.classList.add("edit");
editable.selected = cell;
editable.value = cell.innerHTML;
// (C4) PRESS ENTER/ESC OR CLICK OUTSIDE TO END EDIT
window.addEventListener("click", editable.close);
cell.onkeydown = evt => {
if (evt.key=="Enter" || evt.key=="Escape") {
editable.close(evt.key=="Enter" ? true : false);
return false;
}
};
},
// (D) END "EDIT MODE"
close : evt => { if (evt.target != editable.selected) {
// (D1) CANCEL - RESTORE PREVIOUS VALUE
if (evt === false) {
editable.selected.innerHTML = editable.value;
}
// (D2) REMOVE "EDITABLE"
window.getSelection().removeAllRanges();
editable.selected.contentEditable = false;
// (D3) RESTORE CLICK LISTENERS
window.removeEventListener("click", editable.close);
let cell = editable.selected;
cell.onkeydown = "";
cell.ondblclick = () => editable.edit(cell);
// (D4) "UNMARK" CURRENT SELECTED CELL
editable.selected.classList.remove("edit");
editable.selected = null;
editable.value = "";
// (D5) DO WHATEVER YOU NEED
if (evt !== false) {
console.log(cell.innerHTML);
// check value?
// send value to server?
// update calculations in table?
}
}}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment