Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate row colors without considering invisible rows using pure css

Tags:

css

on load my table looks like

 <table id="mytable">
    <tr><td>&nbsp;</td></tr> //odd color
    <tr><td>&nbsp;</td></tr> //even color
    <tr><td>&nbsp;</td></tr> //odd color
    <tr><td>&nbsp;</td></tr> //even color
 </table>

on some action it becomes like

<table id="mytable">
    <tr><td>&nbsp;</td></tr> //odd color
    <tr style="display: none;"><td>&nbsp;</td></tr> //even color
    <tr><td>&nbsp;</td></tr> //odd color
    <tr><td>&nbsp;</td></tr>
    <tr><td>&nbsp;</td></tr>`
 </table>

if 2nd row is not visible 3rd row should show even color

like image 307
Dilleswari kuncham Avatar asked Nov 18 '14 12:11

Dilleswari kuncham


People also ask

How do I change the color of an alternate row in CSS?

A style selector in CSS is used to change the alternative rows. Using the CSS style selector, you can easily modify the color of the alternate rows. The nth-child() selector in CSS takes an even or odd parameter and then changes the color using the background-color property within this style selector.


1 Answers

Try this simple one:

$("#mytable tr").removeClass("odd even");
$("#mytable tr:visible:odd").addClass("odd");
$("#mytable tr:visible:even").addClass("even");
table,
th,
td {
  border: 1px solid black;
}
.odd {
  background-color: #99FFFF;
}
.even {
  background-color: #FFFF99;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='mytable'>
  <tr>
    <td>&nbsp;1111</td>
  </tr>
  <tr style="display: none;">
    <td>&nbsp;2222</td>
  </tr>
  <tr>
    <td>&nbsp;3333</td>
  </tr>
  <tr>
    <td>&nbsp;4444</td>
  </tr>
</table>

http://jsfiddle.net/fLc1aujb/

like image 54
Giannis Grivas Avatar answered Oct 10 '22 04:10

Giannis Grivas