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

257
Visualizações
How include multiple criteria in COUNT ROWS with javascript

I have the next HTML code:



 <table border="1">
  <tr>
    <th>Nº</th>
    <th>NOMBRE</th>
    <th>AREA</th>
  </tr>
  <tbody id="curso">

    <tr>
        <td>1</td>
        <td>Jose</td>
        <td>Gramática</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Luis</td>
        <td>Gramática</td>
    </tr>
    <tr>
        <td>3</td>
        <td>Andrea</td>
        <td>Gramática</td>
    </tr>

    <tr>
        <td>4</td>
        <td>Andrea</td>
        <td>Idiomas</td>
    </tr>

  </tbody>
</table>

<p id="Cantidad"></p>

</body>

This code can count the number of rows using a criteria in the AREA field:


<script>

function ContarFila() {

var cantidadFilas = document.getElementById("curso").rows.length;
 
  let resultados = {};
  let elementos = document.querySelectorAll(
    "table tbody tr > td:nth-child(3)"
  );

  elementos.forEach(elemento => {
    if (resultados.hasOwnProperty(elemento.innerText)) {
      resultados[elemento.innerText]++;
    } else {
      resultados[elemento.innerText] = 1;
    }
  });

  console.log(resultados);
  for (let indice in resultados) {
    document.getElementById("Cantidad").innerHTML =
      resultados['Gramática'];
  };
}

window.onload=ContarFila();

</script>

When loading the page and using the 'Gramática' criteria of the 'AREA' field, the result is 3.

How do I include MULTIPLE CRITERIA with the 'NOMBRE' and 'AREA' field at the same time?

Example:

Using Criteria 1: 'Grammar'

Using Criteria 2: 'Luis'

RESULT: 1

THAK YOU!

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

0

If I understood you correctly, then perhaps this example below can help you:

function getResult(table, search = {}) {
  const tableHeadCells = Array.from(table.tHead.rows[0].cells);
  const tableBodyRows = Array.from(table.tBodies[0].rows);
  const indexes = tableHeadCells.map((el) => el.dataset["label"]); // <= // ['index', 'nombre', 'area']

  const count = tableBodyRows.reduce((acc, row) => {
    isSearched = indexes.every(
      (key, i) => !search[key] || search[key] === row.cells[i].innerText
    );
    return isSearched ? ++acc : acc;
  }, 0);

  return count;
}

const table = document.querySelector('table');
console.log('1 => ', getResult(table, {
  area: 'Gramática'
}));
console.log('2 => ', getResult(table, {
  nombre: 'Luis',
  area: 'Gramática'
}));
console.log('3 => ', getResult(table, {
  index: '3',
  nombre: 'Andrea'
}));
<table border="1">
  <thead>
    <tr>
      <th data-label="index">Nº</th>
      <th data-label="nombre">NOMBRE</th>
      <th data-label="area">AREA</th>
    </tr>
  </thead>
  <tbody id="curso">
    <tr>
      <td>1</td>
      <td>Jose</td>
      <td>Gramática</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Luis</td>
      <td>Gramática</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Andrea</td>
      <td>Gramática</td>
    </tr>
    <tr>
      <td>4</td>
      <td>Andrea</td>
      <td>Idiomas</td>
    </tr>
  </tbody>
</table>

Note that in the header of the table, the columns are indicated by data-attributes data-label in order to understand which column belongs to which keyword. And then, we just count the number of elements in the loop, which completely match the specified requirements for all columns

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