Comparing two html tables

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
serkan sendur serkan sendur is offline Offline Feb 26th, 2009, 11:18 am |
0
compares to tables and highligthts the changes
Quick reply to this message  
JavaScript / DHTML / AJAX Syntax
  1. function CompareTables(table1,table2)
  2. {
  3. var instHasChange = false;
  4. for(var i=0; i < table1.rows.length; i++)
  5. {
  6. var changes =RowExists(table2,table1.rows[i].cells[0].innerHTML,table1.rows[i].cells[1].innerHTML);
  7. if(!changes[0])
  8. {
  9. table1.rows[i].style.backgroundColor = "red";
  10. instHasChange = true;
  11. }
  12. else if(changes[1])
  13. {
  14. table1.rows[i].style.backgroundColor = "orange";
  15. instHasChange = true;
  16. }
  17.  
  18. }
  19. for(var i=0; i < table2.rows.length; i++)
  20. {
  21. var changes = RowExists(table1,table2.rows[i].cells[0].innerHTML,table2.rows[i].cells[1].innerHTML);
  22. if(!changes[0])
  23. {
  24. table2.rows[i].style.backgroundColor = "#00CC33";
  25. instHasChange = true;
  26. }
  27. else if(changes[1])
  28. {
  29. table2.rows[i].style.backgroundColor = "orange";
  30. instHasChange = true;
  31. }
  32. }
  33. return instHasChange;
  34. }
  35. function RowExists(table,columnName,columnValue)
  36. {
  37. var hasColumnOrChange = new Array(2);
  38. hasColumnOrChange[0] = false;
  39. hasColumnOrChange[1] = false;
  40. for(var i=0; i < table.rows.length; i++)
  41. {
  42. if(table.rows[i].cells[0].innerHTML == columnName)
  43. {
  44. hasColumnOrChange[0] = true;
  45. if(table.rows[i].cells[1].innerHTML != columnValue)
  46. hasColumnOrChange[1] = true;
  47. }
  48.  
  49. }
  50. return hasColumnOrChange;
  51. }
  52.  
  53. //note : tables are passed by reference so you just pass them using document.getElementById("tableID") method.
0
serkan sendur serkan sendur is offline Offline | Feb 26th, 2009
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.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC