1.合并
function autoRowSpan(tbid, row, col) {
var tb = document.getElementById(tbid);
var lastValue = "";
var value = "";
var pos = 1;
for (var i = row; i < tb.rows.length; i++) {
value = tb.rows[i].cells[col].innerText;
if (lastValue == value) {
tb.rows[i].deleteCell(col);
tb.rows[i - pos].cells[col].rowSpan = tb.rows[i - pos].cells[col].rowSpan + 1;
pos++;
} else {
lastValue = value;
pos = 1;
}
}
}
2.根据上一例合并记录合并下一列
function autoRowSpan2(tbid, row, col) {
var tb = document.getElementById(tbid);
var lastValue = "";
var value = "";
var pos = 1;
for (var i = row; i < tb.rows.length; i++) {
var rospan = tb.rows[i].cells[0].getAttribute('rowSpan');
if (rospan > 1) {
lastValue = tb.rows[i].cells[col].innerText;
var index = 1;
for (var j = i + 1; j < tb.rows.length; j++) {
if (index == rospan) break;
value = tb.rows[j].cells[col - 1].innerText;
//alert(value);
if (lastValue == value) {
tb.rows[j].deleteCell(col - 1);
index++;
} else {
lastValue = value;
}
}
tb.rows[i].cells[col].rowSpan = index;
//tb.rows[i].cells[col].rowSpan = index;
}
}
}
function merge() {
var totalRow = $("#table_ZC tbody").find("tr").length;
var totalCol = $("#table_ZC tbody").find("tr").eq(0).find("td").length;
for (var col = totalCol - 7; col >= 0; col--) {
spanNum = 1;
startCell = $("#table_ZC tbody").find("tr").eq(totalRow - 1).find("td").eq(col);
for (var row = totalRow - 1; row >= 1; row--) {
targetCell = $("#table_ZC tbody").find("tr").eq(row - 1).find("td").eq(col);
if (startCell.text() == targetCell.text() && startCell.text() != "") {
spanNum++;
targetCell.attr("rowSpan", spanNum);
startCell.remove();
} else {
spanNum = 1;
}
startCell = targetCell;
}
}
}
3.合并table 的第一个单元格
function merge() {
var totalRow = $(".jjtable tbody").find("tr").length;
spanNum = 1;
startCell = $(".jjtable tbody").find("tr").eq(totalRow - 1).find("td").eq(0);
for (var row = totalRow - 1; row >= 1; row--) {
targetCell = $(".jjtable tbody").find("tr").eq(row - 1).find("td").eq(0);
if (startCell.text() == targetCell.text() && startCell.text() != "") {
spanNum++;
targetCell.attr("rowSpan", spanNum);
startCell.remove();
} else {
spanNum = 1;
}
startCell = targetCell;
}
}