JS合并表格的列

jsp页面上实现表格的竖向合并。

/**
 * 合并表格的列
 * @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;
    }
    for (var j = fCol; j <= eCol; j++) {
        var oRow = oRows[0];
        var rowspan = 1;
        for (var i = 1; i < oRows.length; i++) {
            var oNextRow = oRows[i];
            if (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].merg == -1) {
                oRow.cells[j].parentNode.removeChild(oRow.cells[j]);
            } else { 
                if (oRow.cells[j].merg > 0) {
                    oRow.cells[j].rowSpan = oRow.cells[j].merg;
                }
            }
        }
    }
}

你可能感兴趣的:(js,合并单元格)