Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnockoutJS table incorporating style for alternating row colors

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?

like image 794
Patrick D Avatar asked Apr 29 '13 13:04

Patrick D


People also ask

How to style alternate table rows color using CSS?

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 ...

Why does knockout use jQuery?

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.

How do I set the width of a knockout style?

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.

How do I style every other row element in a table?

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.


2 Answers

You can use virtual elements <!-- ko 'if': thisRowVisible -->.
Working example: http://jsfiddle.net/zs4B2/1/.

like image 161
Vladimir Avatar answered Sep 20 '22 12:09

Vladimir


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

like image 26
Artyom Neustroev Avatar answered Sep 19 '22 12:09

Artyom Neustroev