jquery 合并相邻且内容相同单元格

公用的方法:

//合并内容相同单元格
function mergeTableCell(table_id, table_colnum) {
	_w_table_firsttd = "";
	_w_table_currenttd = "";
	_w_table_SpanNum = 0;
	_w_table_Obj = $("#" + table_id + " tr td:nth-child(" + table_colnum + ")");
	_w_table_Obj.each(function(i) {
		if (i == 0) {
			_w_table_firsttd = $(this);
			_w_table_SpanNum = 1;
		} else {
			_w_table_currenttd = $(this);
			if (_w_table_firsttd.text() == _w_table_currenttd.text()) {
				_w_table_SpanNum++;
				_w_table_currenttd.hide();
				_w_table_firsttd.attr("rowSpan", _w_table_SpanNum);
			} else {
				_w_table_firsttd = $(this);
				_w_table_SpanNum = 1;
			}
		}
	});
}

使用方法:

在jsp页面中调用如下代码段:

 <script type="text/javascript">
//第一个参数代表table的ID,第二个参数表示要合并的列,从1开始
						 mergeTableCell("powergrid",1);
</script>
 

 

 

合并列的公用方法:

function mergeTableRow(id,row,hide){

  var tds=$("#"+id).find("tr").eq(row-1).children();

  var first=null;

  var colspan=-1;

  var current=null;

  tds.each(function(i){

    if(i==0){

      first=$(this);

      colspan=1;

    }else{

      current=$(this);

      if(first.text()==current.text()){

      colspan++;

      if(hide)

       current.hide();

      else

       current.remove();

      first.attr("colspan",colspan);

      }else{

        first=$(this);

        colspan=1;

      }

    }

  })

}

使用方法:

在body中调用如下方法:

<script type="text/javascript"> 
  try
  {
	  mergeTableRow("summaryTable",2,false);
	  mergeTableRow("summaryTable",1,false);
	
	  var len=800+300*(${fn:length(taskItemList)}*1);
	  $("#summaryTable").css("width",len+"px");
  }
  catch(e)
  {
  }  
</script>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值