- 需求
报表开发解决合并列单元格问题
- 核心代码
<script>
debugger;
mc("dnd-example", 0, 0, 0);
function mc(tableId, startRow, endRow, col) {
var tb = document.getElementById(tableId);
if (col >= tb.rows[0].cells.length) {
return;
}
//获取表行数
if (col == 0) { endRow = tb.rows.length - 1; }
for (var i = startRow; i < endRow; i++) {
if (tb.rows[startRow].cells[col].innerText == tb.rows[i + 1].cells[0].innerText || tb.rows[startRow].cells[0].innerText == tb.rows[i + 1].cells[0].innerText) {
if (tb.rows[startRow].cells[col].innerText == tb.rows[i + 1].cells[0].innerText) {
tb.rows[i + 1].removeChild(tb.rows[i + 1].cells[0]);
tb.rows[startRow].cells[col].rowSpan = (tb.rows[startRow].cells[col].rowSpan | 0) + 1;
}
else {
tb.rows[i + 1].removeChild(tb.rows[i + 1].cells[0]);
tb.rows[startRow].cells[0].rowSpan = (tb.rows[startRow].cells[0].rowSpan | 0) + 1;
}
//
if (i == endRow - 1 && startRow != endRow) {
mc(tableId, startRow, endRow, col + 1);
}
}
//当前列不合并,执行下一列
else {
if (col < 2) {
mc(tableId, startRow, i + 0, col + 1);
}
startRow = i + 1;
}
}
}
</script>
- 效果