js 表格合并单元格

 

5列  根据需要可添加 或 删除 strOneTemp  strTwoTemp  strThreeTemp  strFourTemp  strFiveTemp 

//合并单元格 
this.mergeCellsTable = function(){
var strOneTemp = null;
var indexOne = 1
var objOne={};

var strTwoTemp = null;
var indexTwo = 1
var objTwo={};

var strThreeTemp = null;
var indexThree = 1
var objThree={};

var strFourTemp = null;
var indexFour = 1
var objFour={};

var strFiveTemp = null;
var indexFive = 1
var objFive={};

var _$tr = $('#table>tbody>tr');  //对应table的ID
for(var i=0;i<_$tr.length;i++){
if(i<_$tr.length-1){    //不包括最后一行
var flagOne = false;
var flagTwo = false;
var flagThree = false;
var flagFour = false;
var flagFive = false;

var oneStr = $(_$tr[i]).find("td").eq(0).text();
if(oneStr==strOneTemp){
$(objOne).attr("rowspan",indexOne+1);
flagOne = true;
indexOne++;
}else{
objOne = $(_$tr[i]).find("td").eq(0);
strOneTemp = oneStr;
indexOne = 1;
}

var twoStr = $(_$tr[i]).find("td").eq(1).text();
if(twoStr==strTwoTemp){
$(objTwo).attr("rowspan",indexTwo+1);
flagTwo = true;
indexTwo++;
}else{
objTwo = $(_$tr[i]).find("td").eq(1);
strTwoTemp = twoStr;
indexTwo = 1;
}

var threeStr = $(_$tr[i]).find("td").eq(2).text();
if(threeStr==strThreeTemp){
$(objThree).attr("rowspan",indexThree+1);
flagThree = true;
indexThree++;
}else{
objThree = $(_$tr[i]).find("td").eq(2);
strThreeTemp = threeStr;
indexThree = 1;
}

var fourStr = $(_$tr[i]).find("td").eq(3).text();
if(fourStr==strFourTemp){
$(objFour).attr("rowspan",indexFour+1);
flagFour = true;
indexFour++;
}else{
objFour = $(_$tr[i]).find("td").eq(3);
strFourTemp = fourStr;
indexFour = 1;
}

var fiveStr = $(_$tr[i]).find("td").eq(4).text();
if(fiveStr==strFiveTemp){
$(objFive).attr("rowspan",indexFive+1);
flagFive = true;
indexFive++;
}else{
objFive = $(_$tr[i]).find("td").eq(4);
strFiveTemp = fiveStr;
indexFive = 1;
}

if(flagOne){
$(_$tr[i]).find("td").eq(0).hide();
}

if(flagTwo){
$(_$tr[i]).find("td").eq(1).hide();
}

if(flagThree){
$(_$tr[i]).find("td").eq(2).hide();
}

if(flagFour){
$(_$tr[i]).find("td").eq(3).hide();
}

if(flagFive){
$(_$tr[i]).find("td").eq(4).hide();
}
}
}
}

posted @ 2018-06-07 14:59  景、  阅读(497)  评论(0编辑  收藏  举报