Files
intecsync/uebung3/tabellenmanipulation.html
2019-05-19 23:38:31 +02:00

105 lines
4.2 KiB
HTML

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Tabellenmanipulation</title>
<style>
table {
margin-left: auto;
margin-right: auto;
}
table, th, td {
border: 1px solid black;
}
p {
text-align: center;
}
</style>
</head>
<body>
<button id="inscol" onclick="inscolumn()">Spalte hinzufügen</button>
<button id="delcol" onclick="delcolumn()">Spalte entfernen</button>
<button id="insrow" onclick="insrow()">Zeile hinzufügen</button>
<button id="delrow" onclick="delrow()">Zeile entfernen</button>
<p id="dimension"></p>
<table id="table"></table>
<script>
let table = document.getElementById("table");
let tableBody = document.createElement("tableBody");
let counter = document.getElementById("dimension");
let colcount = 0;
let rowcount = 0;
let fieldval = "Feld";
function updateDimension() {
counter.innerHTML = "Dimension: " + rowcount + " x " + colcount;
}
function insrow() {
let row = document.createElement("tr");
tableBody.appendChild(row);
table.appendChild(tableBody);
if(colcount != 0){
for(x=0; x<colcount; x++){
let firstChild = tableBody.children[rowcount];
let cell = document.createElement("td");
let text = document.createTextNode(fieldval);
cell.appendChild(text);
firstChild.appendChild(cell);
}
}
rowcount++;
updateDimension();
}
function delrow(){
if(rowcount < 1){
alert("Es kann keine weitere Zeile entfernt werden.");
} else {
let firstChild = tableBody.children[0];
tableBody.removeChild(firstChild);
rowcount--;
updateDimension();
}
}
function inscolumn(){
if(rowcount==1){
let firstChild = tableBody.children[0];
let cell = document.createElement("td");
let text = document.createTextNode(fieldval);
cell.appendChild(text);
firstChild.appendChild(cell);
} else {
for(i=0; i<rowcount; i++){
let firstChild = tableBody.children[i];
let cell = document.createElement("td");
let text = document.createTextNode(fieldval);
cell.appendChild(text);
firstChild.appendChild(cell);
}
}
colcount++;
updateDimension();
}
function delcolumn(){
if(colcount < 1){
alert("Es kann keine weitere Zeile entfernt werden.");
} else {
for(i=0; i < rowcount; i++){
let firstChild = tableBody.children[i];
firstChild.deleteCell(0);
}
colcount--;
updateDimension();
}
}
</script>
</body>
</html>