Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - check if selected row is last visible row in table

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?

like image 322
owsata Avatar asked Nov 05 '12 15:11

owsata


2 Answers

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');
    }
});​
like image 88
falsarella Avatar answered Sep 28 '22 17:09

falsarella


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.

like image 30
owsata Avatar answered Sep 28 '22 17:09

owsata