views:

1827

answers:

2

Hi,

I have a table with expand and collapse of rows, with column sortable. Below is my code, is there is any ways to improve its performance. And read appending complete group of rows into dom improves performance, but if i do out of $.each() loop it throws error. teble demo

var alt = true;
var altSub = true;

$.each(myData, function(index, row) {

    var noRow = $(row).length;
    var firstRow = $(row[0]);

    for (var i=0; i < noRow; i++) {
        if(firstRow.attr('id') == $(row[i]).attr('id')) {
            if(alt == true) {
                firstRow.removeClass("odd").addClass("even");
                alt = !alt;
                altSub = true;
            } else if( alt == false) {
                firstRow.removeClass("even").addClass("odd");
                alt = !alt;
                altSub = true;
            }
        } else {
            if(altSub == true) {
                $(row[i]).removeClass("alt_row_sub").addClass("alt_row_sub2");
                altSub = !altSub;
            } else if( altSub == false) {
                $(row[i]).removeClass("alt_row_sub2").addClass("alt_row_sub");
                altSub = !altSub;
            }
        }
    }
    $table.children('tbody').append(row);
});

link text

+2  A: 

Tutorials:Zebra Striping Made Easy from the jQuery is a great tutorial on how to do the zebra striping.

Unkwntech
this zebra striping will be applicable in my case. If u look that my demo image, it gives an idea. My table has collapse and expand, because of that have color in group.
vinay
I once heard someone calling it "pajama striping" :-)
Natrium
@vinay quite frankly the image is to damned small I can't see anything on it.
Unkwntech
+3  A: 

You might find the :even and :odd selectors useful.

You could then use them like this:

$('.stripyTable tr:even').addClass('even');
$('.stripyTable tr:odd').addClass('odd');
$('.stripyTable .submenu tr:even').addClass('alt_row_sub');
$('.stripyTable .submenu tr:odd').addClass('alt_row_sub2');

The other thing to consider is whether you can get the different styling of the subsections just with CSS, then in your JS you only need to worry about applying the odd / even classes. The CSS might look something like:

.odd { background-color: blue; }
.even { background-color: white; }
.sub .odd { background-color: green; }
.sub .even { background-color: yellow; }
thatismatt
Thanks for your reply, but its not working properly. it looks like this http://www.freeimagehosting.net/uploads/da55af5e55.gif
vinay
I'd assume that you have some hidden rows which are causing the striping to look wrong. But without seeing your source it is hard to see what's causing that. Using the :visible selector <http://docs.jquery.com/Selectors/visible> when you are applying the striping might help.
thatismatt
Here is some code that will hopefully illustrate my point: http://jsbin.com/iwuqe
thatismatt
Yes, i have expand and collapse rows, one more thing is i cant assign class only to visible rows, if it expands/collapse then again symmetry will not be there.
vinay
Unfortunately the only way that I have found of working around this is to reapply the CSS classes after the rows are shown/hidden.
thatismatt