Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda