首页 > 代码库 > 通用表格合并单元格

通用表格合并单元格

1.源码: 

/**

 * 合并表格的列
 * @param {String} tableId要合并的表格的id
 * @param {int} fCol开始的列
 * @param {int} eCol结束的列
 * @return void
 */
function mergCol(tableId, fCol, eCol) {
var oTable = $(tableId);
if (!$chk(oTable)/*!oTable*/) {
return;
}
var oTBody = oTable.tBodies[0];
var oRows = oTBody.rows;
if (oRows.length == 0) {
return;
}
var rowspan = 1;
for ( var j = fCol; j <= eCol; j++) {
var oRow = oRows[0];
for ( var i = 1; i < oRows.length; i++) {
var oNextRow = oRows[i];
if ((oRow.cells[j] != null && oNextRow.cells[j] != null)
&& (oRow.cells[j].innerHTML == oNextRow.cells[j].innerHTML)) {
rowspan++;
oRow.cells[j].merg = rowspan;
oNextRow.cells[j].merg = -1;
} else {
oRow = oNextRow;
rowspan = 1;
}
}
}
for ( var i = oRows.length - 1; i >= 0; i--) {
for ( var j = eCol; j >= fCol; j--) {
var oRow = oRows[i];
if (oRow.cells[j] != null && oRow.cells[j].merg == -1) {
oRow.cells[j].parentNode.removeChild(oRow.cells[j]);
} else {
if (oRow.cells[j] != null && oRow.cells[j].merg > 0) {
oRow.cells[j].rowSpan = oRow.cells[j].merg;
}
}
}
}
}

应用:mergCol (‘edit-0‘,0,0);

通用表格合并单元格