-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
161 lines (145 loc) · 4.62 KB
/
script.js
File metadata and controls
161 lines (145 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const onlyForDevelop = () => {
document.getElementById("rows").value = "11";
document.getElementById("columns").value = "6";
document.querySelector("input[type='submit']").click();
};
const appendRowsToTbody = (table) => {
const tbody = table.querySelector("tbody") || table.createTBody();
Array.from(table.rows).forEach((row) => {
tbody.appendChild(row);
});
};
const editRecord = (e) => {
const stateCondition = document.querySelector(
"input[name='editRecord']:checked"
)?.id;
if (!stateCondition) return;
const oldElement = e.target;
const colIndex = oldElement.cellIndex;
const row = oldElement.parentElement;
const table = oldElement.closest("table");
const rows = table.rows;
const createNewElement = (tag, text) => {
const newElement = document.createElement(tag);
newElement.textContent = text;
return newElement;
};
const replaceCell = (cell, tag) => {
const newElement = createNewElement(tag, cell.textContent);
cell.replaceWith(newElement);
};
switch (stateCondition) {
case "toggleTHCell":
replaceCell(oldElement, oldElement.tagName === "TD" ? "th" : "td");
break;
case "toggleTHCol":
Array.from(rows).forEach((row) =>
replaceCell(
row.cells[colIndex],
row.cells[colIndex].tagName === "TD" ? "th" : "td"
)
);
break;
case "toggleTHRow":
Array.from(row.cells).forEach((cell) =>
replaceCell(cell, cell.tagName === "TD" ? "th" : "td")
);
break;
case "delCell":
oldElement.remove();
break;
case "delCol":
Array.from(rows).forEach((row) => {
if (colIndex < row.cells.length) {
row.deleteCell(colIndex);
}
});
break;
case "delRow":
row.remove();
break;
case "addCell":
const newCell = row.insertCell(oldElement.cellIndex + 1);
newCell.textContent = `Row ${row.rowIndex + 1}, Col ${
oldElement.cellIndex + 2
}`;
break;
case "addCol":
Array.from(rows).forEach((row, i) => {
const cell = row.insertCell(colIndex + 1);
cell.textContent = `Row ${i + 1}, Col ${colIndex + 2}`;
});
break;
case "addRow":
const newRow = table.insertRow(row.rowIndex + 1);
Array.from(row.cells).forEach((_, i) => {
const cell = newRow.insertCell(i);
cell.textContent = `Row ${row.rowIndex + 2}, Col ${i + 1}`;
});
break;
case "renameAllCells":
Array.from(rows).forEach((row, i) => {
Array.from(row.cells).forEach((cell, j) => {
cell.textContent = `Row ${i + 1}, Col ${j + 1}`;
});
});
break;
case "toggleThead":
appendRowsToTbody(table);
const thead = table.createTHead();
thead.appendChild(row);
break;
case "toggleTfoot":
appendRowsToTbody(table);
const tfoot = table.createTFoot();
tfoot.appendChild(row);
break;
default:
break;
}
document.getElementById("result").value = table.outerHTML;
};
const generateTable = (e) => {
e.preventDefault();
const rows = parseInt(document.getElementById("rows").value, 10);
const columns = parseInt(document.getElementById("columns").value, 10);
const captionData = document.getElementById("caption").value.trim();
const tableContainer = document.getElementById("tableContainer");
const resultTextarea = document.getElementById("result");
tableContainer.innerHTML = "";
resultTextarea.value = "";
const table = document.createElement("table");
if (captionData) {
const caption = document.createElement("caption");
caption.textContent = captionData;
table.appendChild(caption);
}
for (let i = 0; i < rows; i++) {
const tr = document.createElement("tr");
tr.addEventListener("click", editRecord);
for (let j = 0; j < columns; j++) {
const td = document.createElement("td");
td.textContent = `Row ${i + 1}, Col ${j + 1}`;
tr.appendChild(td);
}
table.appendChild(tr);
}
tableContainer.appendChild(table);
resultTextarea.value = table.outerHTML;
};
const toggleTableContainer = () => {
const editContainer = document.getElementById("editContainer");
const tableContainer = document.getElementById("tableContainer");
editContainer.style.display = document.getElementById("showTable").checked
? "grid"
: "none";
tableContainer.style.display = document.getElementById("showTable").checked
? "block"
: "none";
};
const toggleCodeContainer = () => {
const codeContainer = document.getElementById("result");
codeContainer.style.display = document.getElementById("showCode").checked
? "block"
: "none";
};