/**
* 获取单元格修正后的x坐标位置
* @param {Array} tdsPos - 存储单元格信息的二维数组
* @param {number} rowIndex - 行索引
* @param {number} columnIndex - 列索引
* @returns {Object} - 修正后的x坐标位置
*/
function getTdRealPosX(tdsPos, rowIndex, columnIndex) {
let colspanOffset = 0
const currentTdPos = tdsPos[rowIndex][columnIndex]
let flagNum = 0
if (columnIndex !== 0) {
const previousTdPos = tdsPos[rowIndex][columnIndex - 1]
flagNum = previousTdPos.pos.x + previousTdPos.colspan - 1
}
for (let i = rowIndex; i >= 0; i--) {
const rowCols = tdsPos[i]
for (let j = 0; j < rowCols.length; j++) {
const col = rowCols[j]
if (i === rowIndex && j < columnIndex) {
// 同一行单元格对后面单元格x坐标影响
colspanOffset += col.colspan
} else if (i < rowIndex && col.rowspan + i - 1 >= rowIndex) {
if (
col.pos.x + col.colspan - 1 <= flagNum ||
colspanOffset === col.pos.x
) {
// 上面单元格rowspan对x坐标的影响
colspanOffset += col.colspan
}
}
}
}
if (colspanOffset > 0) {
console.log(`单元格 ${currentTdPos.title} 向右偏移 ${colspanOffset}`)
}
return { x: colspanOffset, y: rowIndex }
}
/**
* 获取单元格修正后的坐标位置
* @param {HTMLElement} trParent - tbody元素
* @returns {Array} - 修正后的单元格位置二维数组
*/
function getRealPos(trParent) {
const tdsPos = []
const trs = trParent.children
for (let i = 0; i < trs.length; i++) {
const tr = trs[i]
const tds = tr.children
tdsPos[i] = []
for (let j = 0; j < tds.length; j++) {
const td = tds[j]
const rowSpan = td.getAttribute('rowSpan') ? Number(td.getAttribute('rowSpan')) : 1
const colSpan = td.getAttribute('colSpan') ? Number(td.getAttribute('colSpan')) : 1
tdsPos[i][j] = {
dom: td,
title: td.innerHTML,
rowspan: rowSpan,
colspan: colSpan
}
}
}
// 逐个单元格坐标修复。修复顺序为从上到下,从左到右。即ABCDEFG。。。的顺序修复。
tdsPos.forEach((rowCols, y) => {
rowCols.forEach((col, x) => {
col.pos = getTdRealPosX(tdsPos, y, x)
})
})
return tdsPos
}
/**
* 获取表格单元格信息
* @param {HTMLElement} table - 表格元素
* @returns {Object} - 包含单元格信息的对象
*/
export function getTableCells(table) {
const tbody = table.querySelector('tbody')
const tdsPos = getRealPos(tbody)
const tableCells = {}
// 填充 tableCells 对象
tdsPos.forEach((row, rowIndex) => {
tableCells[rowIndex] = {}
row.forEach((cell, colIndex) => {
tableCells[rowIndex][colIndex] = {
dom: cell.dom,
value: cell.dom.innerHTML,
rowspan: cell.rowspan,
colspan: cell.colspan,
pos: {
colIndex: cell.pos.x,
rowIndex: cell.pos.y
}
}
})
})
return tableCells
}
获取 table 所有单元格的行列号,含合并单元格,行列号纠正
于 2024-05-31 10:18:00 首次发布