Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
Check for duplicates in 2 columns of repeater

I have a table #mytable with 2 columns :

<table id="#mytable">
<tr><td class="name">John</td><td class="surname">Smith</td></tr>
<tr><td class="name">Paul</td><td class="surname">Jackson</td></tr>
<tr><td class="name">Mary</td><td class="surname">Jane</td></tr>
</table>

I also have a repeater subfield, with 2 subfields as well : "name" and "surname". I want to add a class to "#mytable tr" if the name AND surname are contained in the repeater.

I know how to check if one value is contained in one column of the repeater, but I don't know how to check for both values.

Here is what I have for one value :

 jQuery.each($('#mytable tr .name'), function () {
        var line = $(this).closest("tr");
        var name = this.textContent;
        var namesrepeater = $('#myrepeater .namesrepeater select option:selected').map(function() {  return $(this).text();}).get().join(',');
 
    if (namesrepeater.includes(name)) {
      line.addClass("yes");
    }       
  });

Thank you very much in advance !

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So I think I understood your question correctly. Basically, we get a list of names and surnames from the table.

Then, we add an event listener on select so when one changes, we run a callback function.

Within this function, we get the name and surname values of the selects, then see if they exist in the names and surnames lists, then we get the tr with that name and surname using a filter and add a class to it.

const names = []
const surnames = []

// Build a list of names and surnames
$('#mytable tr').each(function () {
  const name = $(this).find('.name').text()
  const surname = $(this).find('.surname').text()

  names.push(name)
  surnames.push(surname)
})

// Add an event listener for when a select changes
$('select').on('change', (e) => {
  // remove all of the classes because none of them may be valid any more
  $('.red').removeClass('red')
  
  const name = $('#name').find('option:selected').val()
  const surname = $('#surname').find('option:selected').val()
  
  // If the name and surname appear in the list of names and surnames
  if (names.indexOf(name) > -1 && surnames.indexOf(surname) > -1) {
    // filter out all of the tr's so we only get the one with the name and surname inside
    const tr = $('tr').filter(function () {
       return $(this).find('.name').text() === name && $(this).find('.surname').text() === surname
    })
    
    // add the class
    tr.addClass('red')
  }
})
tr.red {
  background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="mytable">
  <tr><td class="name">John</td><td class="surname">Smith</td></tr>
  <tr><td class="name">Paul</td><td class="surname">Jackson</td></tr>
  <tr><td class="name">Mary</td><td class="surname">Jane</td></tr>
  <tr><td class="name">John</td><td class="surname">Paul</td></tr>
</table>

<select id="name">
  <option></option>
  <option value="John">John</option>
  <option value="Paul">Paul</option>
</select>

<select id="surname">
  <option></option>
  <option value="Smith">Smith</option>
  <option value="Paul">Paul</option>
</select>

One thing to note is that if you can, you should probably query data attributes rather than using the text value of the td within the tr.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda