0

I am working on collapse-able tables. I am trying to change background for even tr but I don't calculate hidden tr which have .hide class.

Issue: suppose I have two hidden tr by applying

:nth-of-type(even) // even = 2
{
  background: #F5F5F5;
}

changes background of every even tr. This cause two visible tr with same color. While I need stripped functionality. I don't want this selector apply on those hidden tr with class .hide. So that there would be no two adjustment tr with same background. As shown in screen shot.

Fiddle with Issue

enter image description here

I have tried following but not successful.

tr:not(.hide)
{
    &:nth-of-type(even)
    {
      background: #F5F5F5;
    }
}
Aamir Shahzad
  • 6,683
  • 8
  • 47
  • 70

2 Answers2

2

You can't do this in pure css.

Solved a similar issue iterating table and dynamically adding odd/even css rules to each row.

Suppose this tab

<table id="theTab">
  <tr><td>row 1</td></tr>
  <tr class="hidden"><td>row 2</td></tr>
  <tr><td>row 3</td></tr>
  <tr><td>row 4</td></tr>
</table>

With the following JS

var classToggle = false;

$("#theTab tr:not(.hidden)").each(function(){
    $(this).addClass(classToggle ? "odd" : "even");
    classToggle = !classToggle;
});

It's a JQuery based solution, but works fine and doesn't require any table modification (you can use anything instead of .hidden class selector..)

DEMO here

Davide Rossi
  • 516
  • 2
  • 8
0

This is what I did in similar situation. nth-of-type works only with different tag names.

HTML

<div class="table">
   <div class="row">...</row>
   <aside><div class="row">...</row></aside>
   <div class="row">...</row>
   <div class="row">...</row>
</div>

CSS

.table {display:table}
aside {display:none;}
.table .row:nth-of-type(odd) { background: pink; }
kornieff
  • 2,389
  • 19
  • 29