เรียนรู้วิธีสร้างตาราง HTML แบบไดนามิกโดยใช้ HTML, CSS และ JavaScript บทช่วยสอนทีละขั้นตอนนี้จะแนะนำคุณตลอดกระบวนการสร้างตารางแบบโต้ตอบพร้อมฟีเจอร์ต่างๆ เช่น การเพิ่ม การลบ และการแก้ไขแถว
See the Pen Dynamic Table with JavaScript by siammakemoneyonline (@siammakemoneyonline) on CodePen.
HTML
<div class="container">
<h1>Dynamic Tableh1>
<div class="input-container">
<input type="number" id="columns" min="1" placeholder="Number of Columns">
<input type="number" id="rows" min="1" placeholder="Number of Rows">
<button onclick="createTable()">Create Tablebutton>div>
<table id="myTable">
table>
div>
CSS
.container{
width: 60%;
margin: 0 auto;
}
h1 {
margin-top: 0;
text-align: center;
}
.input-container{
display: flex;
justify-content: center;
flex-wrap: wrap;
}
#rows,
#columns {
padding: 10px;
font-size: 16px;
margin-right: 10px;
border-radius: 5px;
border: 1px solid #ccc;
}
#rows:focus,
#columns:focus{
border-color: #66afe9;
outline: 0;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102,175,233,.6);
box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102,175,233,.6);
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
}
button:hover {
background-color: #3e8e41;
}
table {
border-collapse: collapse;
width: 100%;
}
.header-input {
width: 100%;
box-sizing: border-box;
font-weight: bold;
border: none;
outline: none;
text-transform: uppercase;
}
#myTable{
margin-top: 30px;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
.editable-cell {
background-color: #f2f2f2;
outline: none
}
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* Firefox */
input[type=number] {
-moz-appearance: textfield;
}
JS
var tableData = [];
function createTable() {
var table = document.getElementById("myTable");
var columnsInput = document.getElementById("columns");
var rowsInput = document.getElementById("rows");
var columns = parseInt(columnsInput.value);
var rows = parseInt(rowsInput.value);
// Clear existing table
while (table.firstChild) {
table.removeChild(table.firstChild);
}
// Create table header
var headerRow = document.createElement("tr");
for (var i = 0; i < columns; i++) {
var th = document.createElement("th");
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("class", "header-input");
input.setAttribute("placeholder", "Column " + (i + 1));
th.appendChild(input);
headerRow.appendChild(th);
}
table.appendChild(headerRow);
// Create table rows
for (var i = 0; i < rows; i++) {
var rowData = [];
var row = document.createElement("tr");
for (var j = 0; j < columns; j++) {
var cell = document.createElement("td");
cell.setAttribute("contenteditable", "true");
cell.setAttribute("class", "editable-cell");
cell.addEventListener("input", updateCell);
rowData.push("");
row.appendChild(cell);
}
table.appendChild(row);
tableData.push(rowData);
}
}
function updateCell(event) {
var rowIndex = event.target.parentNode.rowIndex - 1;
var columnIndex = event.target.cellIndex;
var value = event.target.textContent.trim();
updateData(rowIndex, columnIndex, value);
}
function updateData(row, col, value) {
tableData[row][col] = value;
}
function deleteRow(row) {
var table = document.getElementById("myTable");
table.deleteRow(row);
tableData.splice(row, 1);
}
function updateRow(row) {
var table = document.getElementById("myTable");
var rowData = tableData[row];
var cells = table.rows[row + 1].cells;
for (var i = 0; i < rowData.length; i++) {
var value = cells[i].textContent.trim();
rowData[i] = value;
}
}
function displayData() {
console.log(tableData);
}