【发布时间】:2019-12-05 11:10:53
【问题描述】:
大家好。如何删除选定的行或选定的列?
我有一张桌子:
它由有输入的行和列组成,在每行对面的右侧有一个按钮 - 删除**-row** 行,每列底部下方有一个按钮 - 删除**-col**柱子。通过单击特定行对面的按钮来帮助删除行,并通过单击特定列下的按钮来删除列。
let array1 = [];
let table1 = document.getElementById('tableOne');
let rows1, columns1;
let tableTfoot = document.getElementById('tableTfoot');
window.onload = function() {
let get = function(id) {
return document.getElementById(id);
};
//table initialization
function createOne(arrayRows1, arrayColumns1) {
[rows1, columns1] = [arrayRows1, arrayColumns1];
for (let i = 0; i < arrayRows1; i++) {
array1[i] = [];
let row = table1.insertRow(-1);
for (let j = 0; j < arrayColumns1; j++) {
array1[i][j] = i + j;
let cell = row.insertCell(-1);
cell.innerHTML = "<input style='width: 50px; height: 50px; text-align: center;' type='text' class='firstTable'>";
}
//display of delete buttons on the right next to each line
for (let i = 0; i < rows1; i++) {
if (i + 1 === rows1) {
let cell = row.insertCell(-1);
cell.innerHTML = "<input style='width: 45px; height: 50px; text-align: center;' type='button' id='delRowsOne' value='-row'>";
}
}
}
//add line
get('addRowsOne').onclick = function() {
let row = table1.insertRow(rows1 - 1);
array1[rows1] = [];
for (let j = 0; j < columns1; j++) {
array1[rows1][j] = rows1 + j;
let cell = row.insertCell(-1);
cell.innerHTML = "<input style='width: 50px; height: 50px; text-align: center;' type='text' class='firstTable'>";
}
//display of delete buttons on the right next to each line
for (let i = 0; i < rows1; i++) {
if (i + 1 === rows1) {
let cell = row.insertCell(-1);
cell.innerHTML = "<input style='width: 45px; height: 50px; text-align: center;' type='button' id='delRowsOne' value='-row'>";
}
}
rows1++;
};
//add column
get('addColumnsOne').onclick = function() {
let tr = [...table1.querySelectorAll('tr')];
array1.forEach(function(r, i) {
r[r.length] = i + r.length;
let cell = tr[i].insertCell(columns1);
cell.innerHTML = "<input style='width: 50px; height: 50px; text-align: center;' type='text' class='firstTable'>";
});
columns1++;
let columnButtonFirstTable = document.getElementById('columnButtonFirstTable');
columnButtonFirstTable.style.marginLeft = columnButtonFirstTable.offsetLeft + 37 + 'px';
//add buttons delete to bottom line
let tr1 = [...tableTfoot.querySelectorAll('tr')];
array1.forEach(function(r, i) {
r[r.length] = i + r.length;
let cell = tr1[i].insertCell(-1);
cell.innerHTML = "<input id='delColumnsOne' type='button' value='-col' style='width: 50px;'>";
});
columns1++;
};
//row delete buttons
get('delRowsOne').onclick = function() {
//
};
//column delete buttons
get('delColumnsOne').onclick = function() {
//
};
}
createOne(2, 2);
};
<div class="col-lg-6" style="float: left;">
<div class="col-lg-12" id="columnButtonFirstTable" style="margin-left: 158px;">
<input id="addColumnsOne" type="button" value="+" style="width: 30px;" />
</div>
<div class="col-lg-12">
<table id="tableOne" style="margin-left:53px;"></table>
<table id="tableTfoot" style="margin-left:21px;">
<tfoot>
<tr>
<td><input id="addRowsOne" type="button" value="+" style="width: 30px;" /></td>
<td><input type="button" value="-col" style="width: 50px;" id="delColumnsOne" /></td>
<td><input type="button" value="-col" style="width: 50px;" id="delColumnsOne" /></td>
</tr>
</tfoot>
</table>
</div>
</div>
删除行:
get('delRowsOne').onclick = function() {
//
};
删除列:
get('delColumnsOne').onclick = function() {
//
};
【问题讨论】:
标签: javascript