js 表格合併單元格

景、發表於2018-06-07

 

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();
}
}
}
}

相關文章