I've searched far and wide for a solution to this conundrum and come up empty-handed. I have a HTML table with a total of 13 columns. The 7th column, labeled Number of Tops, is a sum of columns 8-11: # Short-sleeve, # Long-sleeve, # Sweaters, # Jackets. Each row in the table represents a person's wardrobe. My objective is to have columns 8-11 hidden by default, displaying only columns 1-7 and columns 12 and 13. When the header of the 7th column,
<th>Number of Tops</th>
, is clicked, I want columns 8-11 to expand to reveal detailed information about the breakdown of that total. Additionally, when the columns are expanded, clicking the 7th column's header <th>Number of Tops</th>
should contract the expanded columns 8-11. It would be ideal to incorporate a smooth CSS transition as the columns expand and collapse, but that's just an added bonus.
Here's the HTML structure of the table I'm referring to:
<div class="table">
<table>
<thead>
<tr>
<th>Name</th> <!-- Always visible -->
<th>Address</th> <!-- Always visible -->
<th>Phone Number</th> <!-- Always visible -->
<th>Number of Pants</th> <!-- Always visible -->
<th>Number of Shoes</th> <!-- Always visible -->
<th>Number of Socks</th> <!-- Always visible -->
<!-- Column 7: totals column -->
<th>Number of Tops</th> <!-- Always visible; clickable -->
<th># Short-sleeve</th> <!-- collapsible/expandable -->
<th># Long-sleeve</th> <!-- collapsible/expandable -->
<th># Sweaters</th> <!-- collapsible/expandable -->
<th># Jackets</th> <!-- collapsible/expandable -->
<th>Number of Hats</th> <!-- Always visible -->
<th>Number of Bags</th> <!-- Always visible -->
</tr>
</thead>
<tbody></tbody>
</table>
</div>
When the columns are collapsed, I want the corresponding cell values for columns 8-11 to also collapse as if they had a CSS property of display: none
. The table should resemble a standard 9-column layout until the 7th column header is clicked, triggering columns 8-11 to elegantly expand from the right side, transforming the table into a 13-column display. Clicking the 7th column header again should smoothly collapse columns 8-11 towards the right side, restoring the original 9-column table layout. The column order should remain unchanged throughout this process.
If feasible, I would like to achieve this effect using only HTML, Javascript, jQuery, and CSS. The table in question is a DataTable.
Thank you for your assistance!