Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!