CSDN博客

img qiuchun

一个JAVASCRIPT实用函数,完成合并表格中列中具有相同内容的列单元格的功能

发表于2004/10/9 16:17:00  1380人阅读

分类: 【其它技术】

代码如下:
/*****************************************
名称:MergeTableCell
功能:合并表格中特定列中具有相同内容的列单元格(向下合并)
参数:
TheTable:Table的ID(直接传ID即可,不用加其它父限定)
ColumnIndex:从零开始的要合并列的索引
******************************************/
function MergeTableCell(TheTable,ColumnIndex)
{
 
   var RowCount = TheTable.rows.length;
   var CurrentRow = null;
   var NextRow = null;
   var MergeTag = 0;
   var StartMergeCell = 0;
   var EndMergeCell = 0;
   var NextCell = null;
   var Iter = 0;
  
 
   for(var i=0;i<RowCount;i=Iter)
   {
      MergeTag = 0;
      StartMergeCell = 0;
      EndMergeCell = 0;
      CurrentRow = TheTable.rows(i);
      window.alert(CurrentRow.cells(ColumnIndex).innerText);
      if ((CurrentRow.cells(ColumnIndex) != null)&&(i != RowCount-1))
      {                  
         for(var j=i+1;j<RowCount;j++)
         {
            NextRow = TheTable.rows(j);
            NextCell = NextRow.cells(ColumnIndex);
            var strNext = new String();
            strNext = NextCell.innerText;
            var strCurr =new String();
            strCurr = CurrentRow.cells(ColumnIndex).innerText;
            if ( strNext !=  strCurr)
            {
              break;
            }
            else
            {
               if (j==i+1)
               {
                 StartMergeCell = j;
               }
               EndMergeCell = j;
               MergeTag++;
            }
             
         }
       
         if (MergeTag != 0)
         {
            CurrentRow.cells(ColumnIndex).rowSpan = MergeTag + 1;
            for(var k=StartMergeCell;k<=EndMergeCell;k++)
            {
               TheTable.rows(k).deleteCell(ColumnIndex);
            }
         }
      }
       Iter = Iter + MergeTag + 1 ;
 

   }
      
}


很烂的算法,希望大家不要介意!

阅读全文
0 0

相关文章推荐

img
取 消
img