JQuery selector for a table with hidden row - alternative row coloring

I have a table, and usually I use this selector to apply an odd and even row:

table.find('tbody tr:even').addClass('even');
table.find('tbody tr:odd').removeClass('even');

My table has rows that are inserted in different places, so I delete them from the odd rows.

Now I have some lines hidden with

jQueryTrObject.hide();

I want to apply the same style as before, so that alternating lines, as far as the user is concerned, are marked as odd and even, and I would like it to take into account hidden lines.

How can I write a selector to do this because I have to use each and specifically test?

+3
source share
4 answers

use :visibleselector

table.find('tbody tr.even').removeClass('even');
table.find('tbody tr:visible:even').addClass('even');

, :even .

+6

:

table.find('tbody tr').removeClass('even')
    .filter(':visible:even').addClass('even');
0

Use selector :not(:hidden)

table.find('tbody tr:not(:hidden):even').addClass('even');
0
source

You can use: view selector only for marking the visible line:

table
    .find('tbody tr:visible:even')
    .addClass('even')
.end()
    .find('tbody tr:visible:odd')
    .removeClass('even');
.end();
0
source

Source: https://habr.com/ru/post/1789024/


All Articles