Comparing two html tables

serkan sendur 0 Tallied Votes 2K Views Share

compares to tables and highligthts the changes

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 = "red";
                 instHasChange = true;
            }
            else if(changes[1])
            {
                table1.rows[i].style.backgroundColor = "orange";
                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 = "#00CC33";
                 instHasChange = true;
            }
            else if(changes[1])
            {
                table2.rows[i].style.backgroundColor = "orange";
                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;
   }

//note : tables are passed by reference so you just pass them using document.getElementById("tableID") method.
serkan sendur 821 Postaholic Banned Featured Poster

i forgot to say that this example is for comparing two tables having two columns; column first to be name, and the second to be value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.