jQuery - check if selected row is last visible row in table
Asked Answered
S

3

7

previously i checked whether a row is last row in a table like this in a condition:

$row.is(':last-child')

which works fine.

Now, i have a requirement where some of the rows in the table are hidden - now i have to check whether a specific row is the last visible row in the table.

I tried with this:

$row.is(':visible:last-child')

but not that successfull. Does someone have a clue?

Subliminal answered 5/11, 2012 at 15:15 Comment(2)
should work, just like $("#question").is(':visible:last-child') on this page returns false but $("#question").is(':visible:first-child') returns true. How doesn't it work?Bleb
I have a strange feeling, but I think he may misunderstand what the :visible selector does. It doesn't check if something is off the page (ie. scrolling). It just sees if something is not hidden on the page. A pure shot in the dark but hey, it might be helpful.Copyread
S
2

Thanx everybody for the quick feedback. I could solve it with the following:

$row.parent().find('tr:visible').last().attr('data-id') is $row.attr('data-id')

(it's in a loop)

@falsarella your answer i guess works fine - is a bit complicated, but it works apparently.

Subliminal answered 5/11, 2012 at 16:39 Comment(1)
Nice. I didn't know I could use id comparison, but OK. A fiddle would be great to help other users with the same problem.Inspection
I
3

This doesn't work because the last-child is display: none;.

One approach is to iterate the childs to find the index of the last visible child (see fiddle) :

var $rows = $('td');
var $rowsReverse = $($rows.get().reverse());

var $lastVisibleIndex = -1;
$rowsReverse.each(function(index) {
    if ($lastVisibleIndex == -1 && $(this).is(':visible')) {
        $lastVisibleIndex = $rowsReverse.length - index - 1;
    }
});

$rows.each(function(index) {
    var $row = $(this);
    if (index == $lastVisibleIndex) {            
        $row.addClass('red');
    }
});​
Inspection answered 5/11, 2012 at 16:29 Comment(0)
S
2

Thanx everybody for the quick feedback. I could solve it with the following:

$row.parent().find('tr:visible').last().attr('data-id') is $row.attr('data-id')

(it's in a loop)

@falsarella your answer i guess works fine - is a bit complicated, but it works apparently.

Subliminal answered 5/11, 2012 at 16:39 Comment(1)
Nice. I didn't know I could use id comparison, but OK. A fiddle would be great to help other users with the same problem.Inspection
I
0

You could try

$(element).is(":visible").last();

or

$(element:visible).last();

If that doesn't work, let me know.

Ir answered 5/11, 2012 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.