views:

154

answers:

3

i tried using this:

 var rowCount = $('#locationsTable tbody tr').length - 1;

but the one issue is that you can't tell if there is 1 record in the table or 0 records because, when there are 0 records, jquery datatable plugin shows one extra tr row that says "No records matched"

is there a better way to get actual valid row/record count?

A: 

Firstly, you selector would be slightly more efficient like this:

$('#locationsTable>tbody>tr')

Then enhance you code with a custom selector:

var rowCount = $('#locationsTable>tbody>tr:excludeNoRecordsMatched').length - 1;

The code for which is here:

$(document).ready(function(){
    $.extend($.expr[':'], {
        excludeNoRecordsMatched: function(el) {
            return ($(el).text().indexOf('No records matched') >= 0);
        },
    });
});

Untested, but might work. You could certainly use something like this anyway!

James Wiseman
thanks for the try.. rowcount seems to be working when there are no records but if there are records i am getting -1 for the var rowCount
ooo
A: 

I would probably rely on the specific class of the "no records matched" cell:

// Detect empty cell
if ($('#locationsTable .dataTables_empty').length > 0) return 0;

// Otherwise, count cells
return $('#locationsTable > tbody > tr').length;
Victor Nicollet
+1  A: 

Try this:

var oTable;
$(document).ready(function() {
    oTable = $('#example').dataTable();
    var oSettings = oTable.fnSettings();
    alert( oSettings.fnRecordsTotal() );
    alert( oSettings.fnRecordsDisplay() );
    alert( oSettings.fnDisplayEnd() );

} );
kgiannakakis