我有一组已更改的表格数据。现在我在两个 html 表中并排呈现了两个数据。现在我想突出显示已更改的列,就像在 svn 文件差异中一样。
我尝试这样做,但我需要使其递归但不确定如何:
function CompareTables(table1, table2) {
var instHasChange = false;
for (var i = 0; i < table1.rows.length; i++) {
var changes = RowExists(table2, table1.rows[i].cells[0].innerHTML, table1.rows[i].cells[1].innerHTML);
if (!changes[0]) {
table1.rows[i].style.backgroundColor = "yellow";
instHasChange = true;
} else if (changes[1]) {
table1.rows[i].style.backgroundColor = "yellow";
instHasChange = true;
}
}
for (var i = 0; i < table2.rows.length; i++) {
var changes = RowExists(table1, table2.rows[i].cells[0].innerHTML, table2.rows[i].cells[1].innerHTML);
if (!changes[0]) {
table2.rows[i].style.backgroundColor = "yellow";
instHasChange = true;
} else if (changes[1]) {
table2.rows[i].style.backgroundColor = "yellow";
instHasChange = true;
}
}
return instHasChange;
}
function RowExists(table, columnName, columnValue) {
var hasColumnOrChange = new Array(2);
hasColumnOrChange[0] = false;
hasColumnOrChange[1] = false;
for (var i = 0; i < table.rows.length; i++) {
if (table.rows[i].cells[0].innerHTML == columnName) {
hasColumnOrChange[0] = true;
if (table.rows[i].cells[1].innerHTML != columnValue)
hasColumnOrChange[1] = true;
}
}
return hasColumnOrChange;
}
上面的代码只有在这个表里面没有内表的情况下才有效(我们用这种方式表示db子表的数据)
请帮忙!谢谢!
编辑
我发现了更多对单个表有效的代码,但不确定如何使其适用于表中的表。在此处发布代码
function highLightDiffList() {
for (var k = 0; k < 8; k++) {
var mkrTname = 'table3' + k;
var actTname = 'table4' + k;
var makerTr = document.getElementById(mkrTname).childNodes;
var actualTr = document.getElementById(actTname).childNodes;
var makerStateLen = makerTr.length;
var actualStateLen = actualTr.length;
var actualState = 1;
var makerState = 1;
if (makerStateLen > 1)
makerState = makerTr.item(1).childNodes.item(0).childNodes;
if (actualStateLen > 1)
actualState = actualTr.item(1).childNodes.item(0).childNodes;
var makerTh = document.getElementById(mkrTname).getElementsByTagName("TH").length;
var minLen;
var maxLen;
var flag;
if (makerStateLen > actualStateLen) {
minLen = actualStateLen;
maxLen = makerStateLen;
flag = 1;
} else {
minLen = makerStateLen;
maxLen = actualStateLen;
flag = 2;
}
for (var i = 1; i < minLen; i++) {
for (var j = 0; j < makerTh; j++) {
if (makerTr.item(i).childNodes.item(0).childNodes.item(j).innerHTML != actualTr.item(i).childNodes.item(0).childNodes.item(j).innerHTML) {
makerTr.item(i).childNodes.item(0).childNodes.item(j).style.backgroundColor = document.getElementById('cColor').value;
actualTr.item(i).childNodes.item(0).childNodes.item(j).style.backgroundColor = document.getElementById('cColor').value;
}
}
}
for (var i = minLen; i < maxLen; i++) {
for (var j = 0; j < makerTh; j++) {
if (flag == 1)
makerTr.item(i).style.backgroundColor = document.getElementById('adColor').value;
if (flag == 2)
actualTr.item(i).style.backgroundColor = document.getElementById('adColor').value;
}
}
}
}
function highLightDiff() {
for (var j = 0; j < 6; j++) {
var mkrTname = 'table1' + j;
var actTname = 'table2' + j;
var makerTr = document.getElementById(mkrTname);
var actualTr = document.getElementById(actTname);
var makerStateLen = makerTr.childNodes.item(1).childNodes.length;
var actualStateLen = actualTr.childNodes.item(1).childNodes.length;
var makerState = makerTr.childNodes.item(1).childNodes;
var actualState = actualTr.childNodes.item(1).childNodes;
for (var i = 1; i < makerStateLen; i++) {
if (makerState.item(i).childNodes.item(1).innerHTML != '' && actualState.item(i).childNodes.item(1).innerHTML == '') {
makerState.item(i).childNodes.item(1).style.backgroundColor = document.getElementById('adColor').value;
actualState.item(i).childNodes.item(1).style.backgroundColor = document.getElementById('adColor').value;
} else {
if (makerState.item(i).childNodes.item(1).innerHTML == '' && actualState.item(i).childNodes.item(1).innerHTML != '') {
makerState.item(i).childNodes.item(1).style.backgroundColor = document.getElementById('adColor').value;
actualState.item(i).childNodes.item(1).style.backgroundColor = document.getElementById('adColor').value;
} else {
if (makerState.item(i).childNodes.item(1).innerHTML != actualState.item(i).childNodes.item(1).innerHTML) {
makerState.item(i).childNodes.item(1).style.backgroundColor = document.getElementById('cColor').value;
actualState.item(i).childNodes.item(1).style.backgroundColor = document.getElementById('cColor').value;
}
}
}
}
}
}
内表的创建方式如下:
<table>
<tbody>
<tr>
<td>
<div>
<table>
<tbody>
<tr>
<td>