X龙@China .Net 'blog

需要的不仅仅是工作,而是通过努力得来的美好将来。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

js合并表头(表头为a_b、a_c)

Posted on 2010-04-08 12:02  X龙  阅读(672)  评论(0编辑  收藏  举报
代码

///合并表头
//
/table:要操作的表格
//
/startIndex:要合并的单元格起始索引
//
/startIndex:要合并的单元格结束索引
//
/colCombine:要合并的列数
function CombineTableHeader(table, startIndex, endIndex,colCombine) {
    
var tb = table;
    
var newRow = tb.insertRow(0);

    
//i:插入的单元格索引,k:当前操作的单元格游标,n:表头行的最大单元格数目
    for (var i = 0, k = 0, n = tb.rows[1].cells.length; i < n; i++) {
        
var newCell = newRow.insertCell(i);
        
        newCell.style.backgroundColor
="#E0E0E0";
        newCell.style.textAlign
="center";
        newCell.style.fontWeight
="bold";
        newCell.style.height
="37px";
        
        
if (k >= startIndex && k <= endIndex) {
            
var text = tb.rows[1].cells[k].innerText;
            tb.rows[
0].cells[i].innerText = text.split("_")[0]
            tb.rows[
0].cells[i].colSpan = colCombine;

            
for (var x = 0; x < colCombine; x++) {
                tb.rows[
1].cells[k].innerText = tb.rows[1].cells[k].innerText.split("_")[1];
                k
++;
            }
            n
=n-colCombine+1;
        }
        
else {
            newCell.innerText 
= tb.rows[1].cells[k].innerText;
            tb.rows[
1].deleteCell(k);            
            newCell.rowSpan 
= 2;
            startIndex
--;
            endIndex
--;

        }
    }

}

a b_c b_d e
1 2 3 4

 

结果

a b e
c d
1 2 3 4
点击这里给我发消息http://wp.qq.com/index.html