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

218
Visualizações
Group column with the same subheader name in datatable using Javascript/jQuery

I have a table like this

enter image description here

In which I have different city where we have demand-supply of different products.

Now what I want as here demand is different for all the products However supply is the same on all of three product, so I want that table looks like in this manner.

enter image description here

What I want to do is I want only to show the supply column once in the last of the table. This has to be done dynamically as in the future we have multiple products

Can anyone help me with this?

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

0

What the code below does is:

  • Identify the positions of the "Supply"'s and store them in ind array, in this case will be [3, 5, 7]
  • Loops through ind except for the last element 7(as one "Supply" will be left) and hide all td's; $("td:nth-child("3"), $("td:nth-child("5")
  • The "Demand"s that precede each of these elements will be assigned two spaces.

      let ind = [];
      $("td:contains('Supply')").each(function (index) {
        ind.push($(this).index() + 1);
      });

      $(".hide").on("click", function () {
        for (let i = 0; i < ind.length - 1; i++) {
          let el = $("td:nth-child(" + ind[i] + ")");
          el.prev().attr("colspan", "2");
          el.hide();
        }
      });
      table,
      th,
      td {
        border: 1px solid black;
        border-collapse: collapse;
      }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
      <tr>
        <th>City</th>
        <th colspan="2">Product 1</th>
        <th colspan="2">Product 2</th>
        <th colspan="2">Product 3</th>
      </tr>
      <tr>
        <td></td>
        <td>Demand</td>
        <td>Supply</td>
        <td>Demand</td>
        <td>Supply</td>
        <td>Demand</td>
        <td>Supply</td>
      </tr>
      <tr>
        <td>City 1</td>
        <td>50$</td>
        <td>60$</td>
        <td>90$</td>
        <td>60$</td>
        <td>100$</td>
        <td>60$</td>
      </tr>
      <tr>
        <td>City 2</td>
        <td>50$</td>
        <td>60$</td>
        <td>90$</td>
        <td>60$</td>
        <td>100$</td>
        <td>60$</td>
      </tr>
      <tr>
        <td>City 3</td>
        <td>50$</td>
        <td>60$</td>
        <td>90$</td>
        <td>60$</td>
        <td>100$</td>
        <td>60$</td>
      </tr>
      <tr>
        <td>City 4</td>
        <td>50$</td>
        <td>60$</td>
        <td>90$</td>
        <td>60$</td>
        <td>100$</td>
        <td>60$</td>
      </tr>
    </table>
    <button class="hide">Hide</button>

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