2

I have this table and I've got it set out where quite a few cells in the first column have a rowspan greater than 1. This causes a problem for the styling I use:

tbody > tr:nth-of-type(odd) {
    background-color:#55f
}

How can I get it set up so any row where the first columns cell styling? I have set up a fiddle because I don't exactly know how to explain it.

In the fiddle there are 2 tables, the top is what happens, and the bottom is what I want to happen (without inline styling)

http://jsfiddle.net/qLrauoxc/1/

Spedwards
  • 4,167
  • 16
  • 49
  • 106

2 Answers2

0

You can have multiple tbody in a table. If it makes sense in your case, you could use them to group your tr, and apply the background-color on the tbody:nth-of-type(odd).

GentleFish
  • 349
  • 3
  • 5
0

UPDATE: UPDATED FIDDLE

Using jQuery.

The counter variable will represent all the first <td> of all <tr> on #one <tbody>.
The restricted variable will represent the span of a <td> with a set attribute of rowspan, and the number of <tr>s that will be skipped.
The flag variable will represent the skip(odd or even).

Note: Changing the initial flag to 1, will make the highlighting start at the second that will be found (even).

jQuery:

     $(document).ready(function(){
    var counter = 1;
    var restricted = 0;
    var flag = 0;
    $("#one tbody > tr td:first-child").each(function(){
        var x = $(this).attr("rowspan");
        if(counter > restricted){
        if(flag == 0){
        $(this).parent().css({"background-color":"#f0f"});
        if (typeof x !== typeof undefined && x !== false) { 
            var $selector = $(this).parent();
            for(var i=1;i<x;i++){
                $selector.next('tr').css({"background-color":"#f0f"});
                $selector = $selector.next('tr');
            }
            if(flag == 0){
                flag = 1;
            }
            restricted = restricted + parseInt(x);
        }  
        else{
            restricted = restricted + 1;
            if(flag == 0){
                flag = 1;
            }
        }       
    }
    else if(flag == 1){
        var x = $(this).attr("rowspan");
        if (typeof x !== typeof undefined && x !== false) { 
            restricted = parseInt(restricted) + parseInt(x);
        }    
        else{
            restricted = restricted + 1;
        }
       flag = 0;
    } 
    }
        counter++;        
    });
});

UPDATED JSFIDDLE

reuelab
  • 1,976
  • 1
  • 19
  • 28