jquery隣接するコンテンツが同じセルをマージ

2205 ワード

共通の方法:
//         
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>