I have a table that consists of products. between each row of products there is another row that will contain another table which will be the user’s inventory for that particular product above it.
By default, this inventory row contains the class d-none
to hide the row when it is not expanded. This is because if I set the height of this row to 0 you can still see the row.
When the user clicks to expand the inventory row for a product, I toggle the d-none
class and set the div height to 200px. The below expands the div’s height to 200px instantly and transition that I have set in the CSS is not followed.
css
.inventory {height: 0px; max-height: 200px; overflow-y: auto; transition: height .3s linear;}
.inventory.expanded{height: 200px;}
html
<tr class="data-row">
...
<td class="mb-0 p-1 text-end" style="height: 36px"><h6 class="text-sm mb-0"><em class="table-icon fas fa-chevron-down text-primary cursor-pointer" onclick="expand_card(this)" id="" aria-hidden="true"></em></h6></td>
</tr>
<tr class="mb-0 p-1 d-none" id="inventory-table-">
<td class="bg-gray-100 py-0 px-2" id="" colspan="8" style="vertical-align: top;"><div class="inventory" style="" id="inventory-div-"></div></td>
</tr>
js
function expand_card(e) {
let open = e.classList.contains("open")
if (open) {
document.getElementById('inventory-table-' + e.id).classList.toggle('d-none');
document.getElementById('inventory-div-' + e.id).classList.toggle('expanded')
cards_inventory_table_delete(e.id)
e.classList.remove("open")
} else {
document.getElementById('inventory-table-' + e.id).classList.toggle('d-none');
document.getElementById('inventory-div-' + e.id).classList.toggle('expanded')
cards_read(e.id)
e.classList.add("open")
}
}
If I removed the d-none
class and remove the JavaScript to toggle this class, the transition for the div’s height works as expect but leaves a visible row even when the height is set to zero.
html
<tr class="data-row">
...
<td class="mb-0 p-1 text-end" style="height: 36px"><h6 class="text-sm mb-0"><em class="table-icon fas fa-chevron-down text-primary cursor-pointer" onclick="expand_card(this)" id="" aria-hidden="true"></em></h6></td>
</tr>
<tr class="mb-0 p-1" id="inventory-table-">
<td class="bg-gray-100 py-0 px-2" id="" colspan="8" style="vertical-align: top;"><div class="inventory" style="" id="inventory-div-"></div></td>
</tr>
js
function expand_card(e) {
let open = e.classList.contains("open")
if (open) {
document.getElementById('inventory-div-' + e.id).classList.toggle('expanded')
cards_inventory_table_delete(e.id)
e.classList.remove("open")
} else {
document.getElementById('inventory-div-' + e.id).classList.toggle('expanded')
cards_read(e.id)
e.classList.add("open")
}
}
Why is toggling the display causing the transition not to work?