Javascript合并表格相同内容单元格示例

效果图:

HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>表格相同内容合并</title>
</head>
 
<body>
合并前:
<table width="400" border="1">
  <tr>
    <td>a</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
  </tr>
  <tr>
    <td>a</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>6</td>
  </tr>
  <tr>
    <td>a</td>
    <td>2</td>
    <td>3</td>
    <td>3</td>
    <td>4</td>
    <td> </td>
  </tr>
  <tr>
    <td>b</td>
    <td>3</td>
    <td>4</td>
    <td>6</td>
    <td>7</td>
    <td> </td>
  </tr>
  <tr>
    <td>b</td>
    <td>3</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td> </td>
  </tr>
  <tr>
    <td>cc</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td> </td>
  </tr>
  <tr>
    <td>cc</td>
    <td>2</td>
    <td>3</td>
    <td>5</td>
    <td>5</td>
    <td> </td>
  </tr>
  <tr>
    <td>d</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td> </td>
  </tr>
  <tr>
    <td>e</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td> </td>
  </tr>
</table><br />
合并后:<br />
<table width="400" border="1" id="table1">
  <tr>
    <td>a</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
  </tr>
  <tr>
    <td>a</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>6</td>
  </tr>
  <tr>
    <td>a</td>
    <td>2</td>
    <td>3</td>
    <td>3</td>
    <td>4</td>
    <td> </td>
  </tr>
  <tr>
    <td>b</td>
    <td>3</td>
    <td>4</td>
    <td>6</td>
    <td>7</td>
    <td> </td>
  </tr>
  <tr>
    <td>b</td>
    <td>3</td>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td> </td>
  </tr>
  <tr>
    <td>cc</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
    <td> </td>
  </tr>
  <tr>
    <td>cc</td>
    <td>2</td>
    <td>3</td>
    <td>5</td>
    <td>5</td>
    <td> </td>
  </tr>
  <tr>
    <td>d</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td> </td>
  </tr>
  <tr>
    <td>e</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td> </td>
  </tr>
</table>
</body>
</html>


 

JS:

<script type="text/javascript">
    ///合并表格相同行的内容
    ///tableId:表格ID(最好是tbody,避免把表尾给合并了)
    ///startRow:起始行,没有标题就从0开始
    ///endRow:终止行,此参数是递归时检查的范围,一开始时会自动赋值为最后一行
    ///col:当前处理的列
    function MergeCell(tableId, startRow, endRow, col) {
        var tb = document.getElementById(tableId);
        if (col >= tb.rows[0].cells.length) {
            return;
        }
        //当检查第0列时检查所有行
        if (col == 0) {
            endRow = tb.rows.length - 1;
        }
        for (var i = startRow; i < endRow; i++) {
            //subCol:已经合并了多少列
            var subCol = tb.rows[0].cells.length - tb.rows[startRow].cells.length;
            //程序是自左向右合并,所以下一行一直取第0列
            if (tb.rows[startRow].cells[col - subCol].innerHTML == tb.rows[i + 1].cells[0].innerHTML) {
                //如果相同则删除下一行的第0列单元格
                tb.rows[i + 1].removeChild(tb.rows[i + 1].cells[0]);
                //更新rowSpan属性
                tb.rows[startRow].cells[col - subCol].rowSpan = (tb.rows[startRow].cells[col - subCol].rowSpan | 0) + 1;
                //当循环到终止行前一行并且起始行和终止行不相同时递归(因为上面的代码已经检查了i+1行,所以此处只到endRow-1)
                if (i == endRow - 1 && startRow != endRow) {
                    MergeCell(tableId, startRow, endRow, col + 1);
                }
            } else {
                //起始行,终止行不变,检查下一列
                MergeCell(tableId, startRow, i, col + 1);
                //增加起始行
                startRow = i + 1;
            }
        }
    }
    MergeCell('table1', 0, 0, 0);
</script>



posted @ 2013-08-11 01:48  网事  阅读(429)  评论(0编辑  收藏  举报