23

I'm using the jQuery DataTables plug-in for my HTML table.

Is there a way to get the row count of the number of rows in my table across pages.

For example, if I have 70 rows in my table, and let's say 50 of them get displayed on the 1st page, and 20 on the 2nd page. Is there a way to get the count of 70?

I've tried all the suggestions included in this post: jQuery: count number of rows in a table

This includes:

var rowCount = $('#myTable tr').length;
var rowCount = $('#myTable tr').size();
var rowCount = $('#myTable >tbody >tr').length;
var rowCount = $("#myTable").attr('rows').length;

But all the above suggestions seem to return the number of rows on the existing page (in this case, 50 and not 70).

Community
  • 1
  • 1
Pritish
  • 769
  • 4
  • 13
  • 28

5 Answers5

26

It looks like DataTables is removing the rows that aren't on the current page from the DOM, so you aren't going to be able to count them with a jQuery selector. You'll have to use the DataTables API, specifically the fnGetData function:

$(document).ready(function() {

    // Initialize your table
    var oTable = $('#myTable').dataTable();

    // Get the length
    alert(oTable.fnGetData().length);
} );
Matt Peterson
  • 5,169
  • 4
  • 32
  • 34
  • Thanks, this seems to work. Sorry, but wanted to know something else about datatables too. I'm extracting some of the data from the datatables, and passing them in an AJAX call. How do I make sure that I pass data from all rows and not only those on the first page ? Thanks, Pritish. – Pritish Jul 13 '10 at 15:38
  • @Pritish Check out the docs (http://www.datatables.net/api) for the fnGetData function. With no arguments (as in my example), it is actually returning a 2D array with all of the data from the table in it. – Matt Peterson Jul 13 '10 at 15:51
  • Yeah, took a look at it. alert(oTable.fnGetData()); prints out the HTML entire table data. How would I access individual HTML elements(specially, by name) from it though? – Pritish Jul 13 '10 at 16:19
  • 1
    ...Yeah I can also confirm it seems to not work server-side. Answer below works server-side I believe. –  Feb 01 '19 at 22:14
12

SOLUTION

For DataTables 1.10

Use page.info() to retrieve information about the table as shown below.

Property recordsTotal will hold total number of records in the table.

var table = $('#example').DataTable({
    "initComplete": function(settings, json){ 
        var info = this.api().page.info();
        console.log('Total records', info.recordsTotal);
        console.log('Displayed records', info.recordsDisplay);
    }
});

DEMO

See this jsFiddle for code and demonstration.

NOTES

This solution will work for both client-side and server-side processing modes as long as you use page.info() once data has been retrieved, for example in initComplete callback function.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
  • 1
    This should be solution as I can confirm it works on both server-side and client-side. Other solution only works client-side. –  Feb 01 '19 at 22:20
8

When using server-side paging you can access the total record count like so...

dt.fnSettings()._iRecordsTotal;

If you're using server side paging, you could hook into the fnDrawCallback and update your html whenever the table is drawn...

$('#Table').dataTable({
    "fnDrawCallback": function (oSettings) {
        alert(oSettings._iRecordsTotal);
     }
});
dotjoe
  • 26,242
  • 5
  • 63
  • 77
0

If you are using the Scroller extension, and use search to filter down the rows to a subset, you can get the length of that subset like this

$('#foo').dataTable().fnSettings().aiDisplay.length;
radbyx
  • 9,352
  • 21
  • 84
  • 127
  • 1
    This will show a number of filtered records, if search is applied, instead of total number of records. Besides, it's not recommended to access settings directly anymore, see [this note](http://datatables.net/reference/type/DataTables.Settings#Description), appropriate API methods should be used instead where possible. – Gyrocode.com Sep 10 '15 at 17:31
  • I know that is what it will show, sorry if I was unclear. But point taking, you should not access it like this, but use the API now. Should I delete my answer or should we keep it if it can someway help another not to do it like this? – radbyx Sep 11 '15 at 06:28
  • It works, so you may as well just leave it. You just need to clarify the DataTables version it works for (1.9 - 1.10) and the limitations (number of records with filtering applied, if any). – Gyrocode.com Sep 11 '15 at 15:55
-2

In the JSON response of your datatable ajax call, we get 'iTotalDisplayRecords' as one of the property of that response JSON object. You can directly use the value of 'iTotalDisplayRecords' as number of records in your Data Table.