给定2个单元格元素,我的目标是获得一个元素数组,以便在这两个单元格之间形成一个正方形,如下图所示:
我试着在普通的javascript上构建它。
主要问题是识别最小/最大单元格,特别是当我有rowSpans和colSpans时
我制作了以下代码,当时没有跨度!
makeSquareSelection(cell1, cell2) {
const table = cell1.closest("table");
const x1 = cell1.cellIndex;
const y1 = cell1.parentNode.rowIndex;
const x2 = cell2.cellIndex;
const y2 = cell2.parentNode.rowIndex;
const minX = Math.min(x1, x2);
const minY = Math.min(y1, y2);
let maxX = Math.max(x1, x2);
let maxY = Math.max(y1, y2);
console.log("MINX " + minX);
console.log("MAXX " + maxX);
console.log("MINY " + minY);
console.log("MAXY " + maxY);
for (let y = minY; y <= maxY; y++) {
for (let x = minX; x <= maxX; x++) {
console.log("Selecting " + x + " - " + y);
const cell = table.rows[y].cells[x];
this.state.selectedCells.push(cell);
cell.className += "selectedCell";
}
}
}
小提琴演示问题:
https://jsfiddle.net/h63ojct6/5/
当我有colSpans和rowSpans时,怎么能解决这个问题?
谢谢!