Coder_885522
Coder_885522

Reputation: 23

Highlight differences in row data of HTML table

I have this HTML table in which:

I want to write a function in HTML/Javascript/Jquery to highlight cells with matching data between two rows. We may have more than two rows of data.

So basically, we will have to test rows in set of two i.e. 1st header row will be skipped. compare 2nd to 3rd, then test 4th to 5th, then 6t to 7th and so on.

In the example below, expected value in column 11 (column name: Required?) for Mr. John Popular, is "N", but excel report shows "Y".

For Mr. Bill Smith, i.e. in row 4 and 5 dates are different in "Future Exp Date" column and "date of service" column.

I want to highlight this difference.

  
<table border="1" class="dataframe data">
      <thead>
        <tr style="text-align: right;">
          <th>Test</th>
          <th>first name</th>
          <th>last name</th>
          <th>person id</th>
          <th>contract id</th>
          <th>plan id</th>
          <th>authnumber</th>
          <th>request?</th>
          <th>window type</th>
          <th>date request </th>
          <th>Required?</th>
          <th>Solved?</th>
          <th>More attaintion?</th>
          <th>Status</th>
          <th>Future Exp Date</th>
          <th>Issue resolved?</th>
          <th>date of service</th>
          <th>date of exp</th>
          <th>date of system</th>
          <th>Receipt date</th>
          <th>TAT1</th>
          <th>DaysTAT1</th>
          <th>TAT2</th>
          <th>DaysTAT2</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Correct data</td>
          <td>John</td>
          <td>Popular</td>
          <td>759876</td>
          <td>J4856</td>
          <td>642</td>
          <td>837522</td>
          <td>A</td>
          <td>FO</td>
          <td>2019-01-14</td>
          <td>N</td>
          <td>N</td>
          <td>NA</td>
          <td>Approved</td>
          <td>2019-01-14</td>
          <td>NA</td>
          <td>NA</td>
          <td>NA</td>
          <td>2019-01-14</td>
          <td>NA</td>
          <td>0.0</td>
          <td>Y</td>
          <td>NA</td>
          <td>N</td>
        </tr>
        <tr>
          <td>Wrong Data</td>
          <td>John</td>
          <td>Popular</td>
          <td>759876</td>
          <td>J4856</td>
          <td>642</td>
          <td>837522</td>
          <td>A</td>
          <td>FO</td>
          <td>2019-01-14</td>
          <td>Y</td>
          <td>N</td>
          <td>NA</td>
          <td>Approved</td>
          <td>2019-01-14</td>
          <td>NA</td>
          <td>NA</td>
          <td>NA</td>
          <td>2019-01-14</td>
          <td>NA</td>
          <td>0.0</td>
          <td>Y</td>
          <td>NA</td>
          <td>N</td>
        </tr>
        <tr>
          <td>Correct data</td>
          <td>Bill</td>
          <td>Smith</td>
          <td>64963</td>
          <td>J6291</td>
          <td>642</td>
          <td>837522</td>
          <td>A</td>
          <td>FO</td>
          <td>2019-01-14</td>
          <td>N</td>
          <td>N</td>
          <td>NA</td>
          <td>Approved</td>
          <td>2019-01-17</td>
          <td>NA</td>
          <td>NA</td>
          <td>NA</td>
          <td>2019-01-18</td>
          <td>NA</td>
          <td>0.0</td>
          <td>Y</td>
          <td>NA</td>
          <td>N</td>
        </tr>
        <tr>
          <td>Wrong Data</td>
          <td>Bill</td>
          <td>Smith</td>
          <td>64963</td>
          <td>J6291</td>
          <td>642</td>
          <td>837522</td>
          <td>A</td>
          <td>FO</td>
          <td>2019-01-17</td>
          <td>N</td>
          <td>N</td>
          <td>NA</td>
          <td>Approved</td>
          <td>2019-01-18</td>
          <td>NA</td>
          <td>NA</td>
          <td>NA</td>
          <td>2019-01-14</td>
          <td>NA</td>
          <td>0.0</td>
          <td>Y</td>
          <td>NA</td>
          <td>N</td>
        </tr>
      </tbody>
    </table>
    <br>
    <button>Check constraints</button>
    

Upvotes: 1

Views: 977

Answers (1)

fdomn-m
fdomn-m

Reputation: 28611

I have a table which shows data from database (correct data) in the first row. Second row shows erroneous data from excel (wrong data).

I want to make it easy for the end use to see differences in database data and excel data by highlighting cells

Taking the original code and 2 rows, remove the cells part as it checks along the row instead of down the column. Can also remove looping rows as there's only two rows so it's a case of comparing row[0][col] with row[1][col]

With some other tweaks such as removing confusing .toggleClass(class, true) and a slight tweak to the class name / button caption to be clearer what they do, gives:

function highlight(){    
    $(arguments).addClass('changed');
}
function checkConstraints(e){
    // reset style before re-checking
    $('td.changed').removeClass('changed');
    
    // get rows as an array of array
    var rows = $('tbody>tr').map(function(elem,i){
        return [$(this).children('td').toArray()];
    }).toArray();        
    
    // only 2 rows, so no need to loop them
    // start at j=1 to skip first column
    for(var j = 1; j < rows[0].length; j++){

      if (rows[0][j].innerText != rows[1][j].innerText) 
      {               
         highlight(rows[0][j],rows[1][j]);
      }            
    }        
}
$('button').click(checkConstraints);
td {
    width:100px;
    height:50px;
    border:1px solid black;
}
table {
    border:1px solid black;
    border-collapse:collapse;
}
td.changed {
    background:orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1" class="dataframe data">
  <thead>
    <tr style="text-align: left;">
      <th>Test</th>
      <th>first name</th>
      <th>last name</th>
      <th>person id</th>
      <th>contract id</th>
      <th>plan id</th>
      <th>authnumber</th>
      <th>date request </th>
      <th>Required?</th>
      <th>Solved?</th>
      <th>More attaintion?</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Correct data</td>
      <td>John</td>
      <td>Popular</td>
      <td>759876</td>
      <td>J4856</td>
      <td>642</td>
      <td>837522</td>
      <td>2019-01-14</td>
      <td>N</td>
      <td>N</td>
      <td>NA</td>
      <td>Approved</td>
    </tr>
    <tr>
      <td>Wrong Data</td>
      <td>John</td>
      <td>Wick</td>
      <td>759876</td>
      <td>J4856</td>
      <td>-642</td>
      <td>837522</td>
      <td>2019-01-14</td>
      <td>Y</td>
      <td>N</td>
      <td>NA</td>
      <td>Approved</td>
    </tr>
  </tbody>
</table>
<br>
<button>Compare Rows</button>

Upvotes: 2

Related Questions