I typically zebra stripe table rows for odd / even like so and it works well:
$("table tbody tr:visible:even",this).addClass("even");
$("table tbody tr:visible:odd",this).addClass("odd");
However, I have a data table where there are three consecutive rows for 1 set of data. The next three consecutive rows would be for the next set of data. So ideally I’d like to take the first three rows and add a class of even and then the next three rows after that to have a class of odd.
Here’s something I whipped up on jsfiddle:
What this does is select every 6th
trelement, set its class toodd, and the same to the previous twotrelements, thus giving you the result of 3 “grouped” rows.More about the
nth-child()selector here, and more about theprev()function here.You could change the code to this to add an
evenclass to the three rows preceding the ones with theoddclassname:That looks like this.