I'm using KnockoutJS in my HTML to data-bind the visibility of rows of a table to certain observable values, as defined in my accompanying JavaScript. My table looks like this:
<table class="myTable">
<tr data-bind="if: thisRowVisible"> <!-- Arbitrary data here --> </tr>
<tr data-bind="if: thatRowVisible"> <!-- Arbitrary data here --> </tr>
<tr data-bind="if: anotherRowVisible"> <!-- Arbitrary data here --> </tr>
<!-- More rows defined here -->
</table>
As the application runs, the rows of the table may be hidden or shown, by using these data-bound if
values. In order to give the rows of the table alternating colors (zebra/striping), I have the following defined in my CSS:
.myTable tr:nth-child(even) td {
background-color: black;
}
.myTable tr:nth-child(odd) td {
background-color: gray;
}
Normally, this CSS would style the rows properly. The even rows would be colored black, and the odd rows would be colored gray. However, my problem occurs when you throw in the Knockout data-bindings.
For instance, let's say the Index #2 row is hidden, as a result of my data-binding. Even though the row is hidden, my <tr>
definition for that row still exists in the table. This throws off the alternating row colors. Since Index #2 still exists, but is hidden, it is still included in the alternating row colors. This means that two gray rows will appear on top of each other.
Is there any to achieve the proper alternating table row colors, while still using my KnockoutJS pattern? Is there some trick to the KO data-binding to remove the hidden <tr>
from Markup completely, so that the CSS styles are applied correctly?
In this tutorial, learn how to style alternate table rows color using CSS. The short answer is: use the CSS property :nth-child () selector to apply and highlight the background color of alternate table rows. You can pass the numbers (like 1,2,3…) or number expression (like n, n+1, 2n+1….) or any expressions or even/odd as the argument of ...
Knockout will use jQuery’s css function to set the styles, if available. This lets you take advantage of the extra compatibility features of jQuery, such as setting browser-specific prefixes.
If you apply a simple numeric value to a style that requires a unit, Knockout will append px to the value before setting the style. For example, style: { width: 100 } will set the width to 100px.
To style every other table row element, use the :nth-child (even) selector like this: Note: If you use (odd) instead of (even), the styling will occur on row 1,3,5 etc. instead of 2,4,6 etc.
You can use virtual elements <!-- ko 'if': thisRowVisible -->
.
Working example: http://jsfiddle.net/zs4B2/1/.
You can assign a specific class to only visible elements and apply zebra styles only for that class. The data binding would be like the following:
<table class="myTable">
<tr data-bind="if: thisRowVisible, attr: {'class': thisRowVisible ? 'rowVisible' : 'rowInvisible' "> <!-- Arbitrary data here --> </tr>
<tr data-bind="if: thatRowVisible, attr: {'class': thatRowVisible ? 'rowVisible' : 'rowInvisible'"> <!-- Arbitrary data here --> </tr>
<tr data-bind="if: thatIsNotVisible, attr: {'class': thatIsNotVisible ? 'rowVisible' : 'rowInvisible'"> <!-- Arbitrary data here --> </tr>
<!-- More rows defined here -->
</table>
And the CSS:
.myTable tr.rowVisible:nth-child(even) td {
background-color: black;
}
.myTable tr.rowVisible:nth-child(odd) td {
background-color: gray;
}
FIDDLE EXAMPLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With