I have a table ".report" and it should have alternating background for its rows. The UI allows users to hide and show rows dynamically and I want the background of the rows to keep the pattern at all times. I implemented the solution where I add the ".hidden" class to the hidden rows and set the background-color attr to shown rows only and then set the background to "nth-of-type" accordingly. But it doesn’t work. It seen the nth-of-type doesn’t take the ":not(.hidden)" rule into consideration. Is it possible to resolve this with CSS only? I know I can change the background with JS but I’d rather avoid it and use css only if possible.
restoreRow: function(key) {
let row = $("#" + key);
row.show();
row.removeClass("hidden");
},
removeRow: function(key) {
let row = $("#" + key)
row.hide();
row.addClass("hidden");
},
.report tbody tr:not(.hidden):nth-of-type(even) {
background-color: $white;
}
.report tbody tr:not(.hidden):nth-of-type(odd) {
background-color: red;
}
2
Answers
I don’t think there is a
only-css
solution to do that, you need to consider theJS
refreshRowStyles
do the style for all thetr
that doesn’t havehidden
class.Call this method when the DOM initialized.
Also, you need to call it with
restoreRow
andremoveRow
methods to ensure the styles are updated every time a row’s visibility is changed.Yes, with the new
nth-child(n-of-x)
selector