Mr.Web
Mr.Web

Reputation: 7154

jQuery: Select identical numbers from a table

I have this HTML table loaded from MYSQL, every row is composed this way:

<tr><td>02/05/2015</td><td class="bari1">89.30.86.72.65</td></tr>
<tr><td>30/04/2015</td><td class="bari2">56.11.73.36.13</td></tr>

Each row has a class 'bari' followed by an id (1 thru 15).

This is the result:

enter image description here

Thru jQuery I created an array of numbers from the last 2 lines, so the array is now composed of: ["9", "1", "43", "79", "28", "40", "16", "6"] (removed the repeating number).

I have to find and highlight the repeating numbers are present in the other columns.

So I did this:

function highlightNumbers(){
  var due_serie = $('.bari15').html().split('.').concat($('.bari14').html().split('.'));
  //Rimuovo i doppioni
  var serieCompleta = [];
  $.each(due_serie, function(i, el){
    if($.inArray(el, serieCompleta) === -1) serieCompleta.push(el);
  });

  //Ottengo dati            
  var ba13 = $('.bari13').html().split('.');
  $('.bari13').html('');
  var found = 0;
  for(i = 0; i<= ba13.length-1; i++){
    for(n = 0; n<= serieCompleta.length-1; n++){
      if(ba13[i] == serieCompleta[n]){
        $('.bari13').append('<span style="color:red">'+ba13[i]+'</span>.');
        found = ba13[i];
      }
    }
    if(ba13[i] != found){
      $('.bari13').append(ba13[i]+'.');
    }
  }
}

With this code I highlighted the numbers in row #13, but I cannot think of a for loop to complete this for the whole table (15 rows).

Upvotes: 1

Views: 75

Answers (2)

Puneet
Puneet

Reputation: 2051

Try This,

I have updated your own code, so that you can understand it easily.

function highlightNumbers(){
  
        var all= $("td[class*=bari]");
        var index = all.length-1;
  
        var due_serie = $(all[index]).html().split('.').concat($(all[index-1]).html().split('.'));
        //Rimuovo i doppioni
        var serieCompleta = [];
        $.each(due_serie, function(i, el){
            if($.inArray(el, serieCompleta) === -1) serieCompleta.push(el);
        });

        //Ottengo dati          
  
        for(var s=0;s<index-1;s++)
          {
            
            var bar = $(all[s]);
            var barnum = bar.html().split('.');
            bar.html('');

            var found = 0;

            for(i = 0; i<= barnum.length-1; i++)
             {
               for(n = 0; n<= serieCompleta.length-1; n++)
                 {
                  if(barnum[i] == serieCompleta[n])
                    {
                    bar.append('<span style="color:red">'+barnum[i]+'</span>.');
                    found = barnum[i];
                    }
                 }
               if(barnum[i] != found)
               {
                bar.append(barnum[i]+'.');
               }
             }
            
          }
  
  
}

highlightNumbers();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><td>02/05/2015</td><td class="bari1">89.10.86.30.65</td></tr>
<tr><td>30/04/2015</td><td class="bari2">96.11.73.36.13</td></tr>
<tr><td>02/05/2015</td><td class="bari3">78.34.50.72.11</td></tr>
<tr><td>30/04/2015</td><td class="bari4">34.78.69.60.22</td></tr>
<tr><td>02/05/2015</td><td class="bari5">12.29.30.69.33</td></tr>
<tr><td>30/04/2015</td><td class="bari6">59.10.20.96.44</td></tr>
</table>

Upvotes: 4

Mladen Oršolić
Mladen Oršolić

Reputation: 1422

I think you could use $.inArray() function, to select only rows that contain any of the numbers in your array.

Upvotes: 0

Related Questions