3

I'm trying to get the count of the number of rows on the current dataTables page. If I do this:

alert($('.tableViewer tbody tr').length)

It gives me the a non-accurate row count (I think it adds the current page and the last one?).

Anyway, I'm just trying to get the row count on the page I'm actually on. Does anyone know how to do this?

Here's my delete button:

function fnDelete(elem){
    if (selected.length>0) {
        var c;
        c = confirm('Are you sure you want to delete the selected ${displayTableName}?');
        if (c) {
            // Create delete url from editor url...
            var deleteURL = (urlstr.substring(0, urlstr.lastIndexOf('/') + 1)) + "delete.do";
            alert($('.tableViewer tbody tr').length)
            deleteRecord(deleteURL,selected[0]);

            alert($('.tableViewer tbody tr').length)
            if ( $('.tableViewer tbody tr').length === 1) {
                setTimeout(function() { oTable.fnPageChange('last'); }, 100);
            }
        }
    }
}
3
  • 1
    Is it possible that there are hidden rows? $('.tableViewer tbody tr:visible').length Commented Oct 26, 2012 at 17:53
  • you are doing the right way actually. inspect your dom elements Commented Oct 26, 2012 at 17:55
  • @JoeFletch -- that was exactly what I was looking for! Thank you very much! If you write it in an answer I'll be sure to pick it as the correct one and upvote you as well. This was driving me crazy! Commented Oct 26, 2012 at 17:56

1 Answer 1

6

The tr elements may be on the page, but not visible. Try this!

$('.tableViewer tbody tr:visible').length
Sign up to request clarification or add additional context in comments.

1 Comment

Keep in mind that even if there are no records to show in the table, there would still be one visible row that says 'No matching records found'. The following returns false if no records exist: $('.tableViewer tbody tr:first td').hasClass( 'dataTables_empty' )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.