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

210
Visualizações
Except for the last country column, hide all columns whose header name begins with country using Javascript/jQuery

This is a table where I need to delete duplicate columns of Country. I want to do this dynamically. Like if I have more than three columns of country, for example, it will automatically hide when there are more than one column of country. T

<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Contact</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Maria Anders</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Francisco Chang</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Roland Mendel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>Helen Bennett</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Yoshi Tannamuri</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Giovanni Rovelli</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

Output should be like this: enter image description here

Can anybody help me with this I am new to coding?

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

0

This hide them:

      let col = [];
      let res = [];
      let ind = [];
      $("th").each(function () {
        col.push($(this).text());
      });

      res = [...new Set(col)];
      for (let i = 0; i < col.length; i++) {
        if (col.indexOf(res[i]) !== -1) {
          ind.push(col.indexOf(res[i]) + 1);
        }
      }

      let diff = Array.from({ length: col.length }, (_, i) => i + 1).filter((x) => !ind.includes(x));

      $(".hide").on("click", function () {
        for (let i = 0; i < diff.length; i++) {
          $("th:nth-child(" + diff[i] + ")").hide();
          $("td:nth-child(" + diff[i] + ")").hide();
        }
      });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Contact</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Maria Anders</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Francisco Chang</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Roland Mendel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>Helen Bennett</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Yoshi Tannamuri</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Giovanni Rovelli</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
    </table>
    <button class="hide">Hide</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

There you go, here is a solution using only css. Note: This is not a global solution, it should work just for your case. Otherwise it needs to be tweeked a little bit for more flexibility.

body {
  font-family: system-ui, sans-serif;
}

table {
  border: 1px solid gray;
  width: 100%;
  table-layout: fixed;
}

table th,
table td {
  padding: .5em .75em;
  text-align: left;
  cursor: default;
}

table th {
  background-color: #c4c4c4;
}

table tr:nth-child(2n+3) td {
  background-color: #eee;
}

table tr th:nth-child(2),
table tr td:nth-child(2),
table tr th:nth-child(3),
table tr td:nth-child(3){
  display: none;
}
<table cellspacing="0" cellpadding="0" border="0">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Contact</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Maria Anders</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Francisco Chang</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Roland Mendel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>Helen Bennett</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Yoshi Tannamuri</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Giovanni Rovelli</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

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