js 拼接html 表格,js合并table单元格(拼table的时候并不知道具体几行几列)
Sys.Application.add_load(function () {
var tab = document.getElementById("ctl00_ContentPlaceHolder1_viewcontrolTT_Repeater1_ctl00_viewcontrolTT_grid"); //要合并的tableID
if (!tab) {
alert("未獲取到表格!");
}
else {
//從第二行開始,排除標(biāo)題行
var startRow = 1;
//循環(huán)記錄表格中td的內(nèi)容,用來判斷td中的value是否發(fā)生了改變
var td1TempV = "";
var td2TempV = "";
var td3TempV = "";
//如果td的值相同,那么變量加1, 否則將臨時(shí)變量加入集合中
var rowCount1 = 1;
var rowCount2 = 1;
var rowCount3 = 1;
//得到相同內(nèi)容的行數(shù)的集合www.2cto.com
var totalcount1 = new Array();
var totalcount2 = new Array();
var totalcount3 = new Array();
for (var i = 1; i < tab.rows.length; i++) {
//首先拿出來td的值
var td1Text = tab.rows[i].cells[0].innerText;
var td2Text = tab.rows[i].cells[1].innerText;
var td3Text = tab.rows[i].cells[2].innerText;
//如果是第一次走循環(huán),直接continue;
if (i == startRow) {
td1TempV = td1Text;
td2TempV = td2Text;
td3TempV = td3Text;
continue;
}
//如果當(dāng)前拿出來的值和出處的值相同,那么將臨時(shí)數(shù)量加1,否則添加到集合里面
if (td1TempV == td1Text && td1Text != null && td1Text != "") {
rowCount1++;
} else {
totalcount1.push(rowCount1);
td1TempV = td1Text;
rowCount1 = 1;
}
if (td2TempV == td2Text && td2Text != null && td2Text != "") {
rowCount2++;
} else {
totalcount2.push(rowCount2);
td2TempV = td2Text;
rowCount2 = 1;
}
if (td3TempV == td3Text && td3Text != null && td3Text != "") {
rowCount3++;
} else {
totalcount3.push(rowCount3);
td3TempV = td3Text;
rowCount3 = 1;
}
//判斷是否是循環(huán)的最后一次,如果是最后一次那個(gè)直接將當(dāng)前的數(shù)量存儲(chǔ)到集合里面
if (i == tab.rows.length - 1) {
totalcount1.push(rowCount1);
totalcount2.push(rowCount2);
totalcount3.push(rowCount3);
}
}
//臨時(shí)變量,再循環(huán)中判斷是否和數(shù)組中的一項(xiàng)值相同
var tNum1 = 0;
var tNum2 = 0;
var tNum3 = 0;
//注意這個(gè)循環(huán)是倒著來的
for (var i = tab.rows.length - 1; i >= startRow; i--) {
//臨時(shí)變量,存儲(chǔ)td
var tTd1 = tab.rows[i].cells[0];
var tTd2 = tab.rows[i].cells[1];
var tTd3 = tab.rows[i].cells[2];
tNum1++;
tNum2++;
tNum3++;
if (tab.rows.length == 2) {
continue;
}
//如果發(fā)現(xiàn)tNum和數(shù)組中最后一個(gè)值相同,那么就可以斷定相同的td已經(jīng)結(jié)束[只是其中一個(gè)]
if (tNum1 == totalcount1[totalcount1.length - 1]) {
//給當(dāng)前td添加rowSpan屬性
tTd1.setAttribute("rowSpan", totalcount1[totalcount1.length - 1]);
tTd1.setAttribute("style", "background-color:#ffffff");
//將數(shù)組的最后一個(gè)元素彈出
totalcount1.pop();
tNum1 = 0;
} else {
//刪除當(dāng)前td
tab.rows[i].removeChild(tTd1);
}
if (tNum2 == totalcount2[totalcount2.length - 1]) {
//給當(dāng)前td添加rowSpan屬性
tTd2.setAttribute("rowSpan", totalcount2[totalcount2.length - 1]);
tTd2.setAttribute("style", "background-color:#ffffff");
//將數(shù)組的最后一個(gè)元素彈出
totalcount2.pop();
tNum2 = 0;
} else {
//刪除當(dāng)前td
tab.rows[i].removeChild(tTd2);
}
if (tNum3 == totalcount3[totalcount3.length - 1]) {
//給當(dāng)前td添加rowSpan屬性
tTd3.setAttribute("rowSpan", totalcount3[totalcount3.length - 1]);
tTd3.setAttribute("style", "background-color:#ffffff");
//將數(shù)組的最后一個(gè)元素彈出
totalcount3.pop();
tNum3 = 0;
} else {
//刪除當(dāng)前td
tab.rows[i].removeChild(tTd3);
}
}
}
});
總結(jié)
以上是生活随笔為你收集整理的js 拼接html 表格,js合并table单元格(拼table的时候并不知道具体几行几列)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html画布显示PPT,【Web前端问题
- 下一篇: 计算机科学在航空航天仿真模拟,2016年