943,935 Members | Top Members by Rank

Ad:
0

Comparing two html tables

by on Feb 26th, 2009
compares to tables and highligthts the changes
JavaScript / DHTML / AJAX Code Snippet (Toggle Plain Text)
  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.
Comments on this Code Snippet
Feb 26th, 2009
0

Re: Comparing two html tables

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.
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: How to rename/hide the title of the popup window
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How to hide white space of a Frame?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC