13

I am using a datatable for my application. How do I get the total count of the rows in the datatable onload? My code:

$(document).ready( function() {
  $('#jobSearchResultTable').dataTable({
    responsive: true,
    "scrollY": 500,
    "scrollCollapse": true,
    "jQueryUI": true,
    "aaSorting": []
  });
)};

How to get the total count of rows in the datatable onload

wazz
  • 4,953
  • 5
  • 20
  • 34
lucifer
  • 2,297
  • 18
  • 58
  • 100

10 Answers10

28

If you're using the new DataTables (v1.10+), you don't have access to some legacied functions such as fnGetData().

Instead, try this:

var table = $('#table_id').DataTable();
var table_length = table.data().count();

If you're using paging (which I was), and need the total count across all pages, try this:

var table = $('#table_id').DataTable({
    'paging': true
});

var length = table.page.info().recordsTotal;

Sources:

https://datatables.net/reference/api/count()

https://datatables.net/reference/api/page.info()

jpaik
  • 281
  • 3
  • 5
17

Update for New Versions

table.data().count()

Reference:https://datatables.net/reference/api/count()

For older versions:

$(document).ready(function() {
 //Initialize your table
 var table = $('#jobSearchResultTable').dataTable();
 //Get the total rows
 alert(table.fnGetData().length);
});

Source: https://stackoverflow.com/questions/3238047/jquery-datatables-row-count-across-pages

Another method:

table.fnSettings().fnRecordsTotal();

see which one works for you

Source: http://datatables.net/forums/discussion/2278/how-to-get-number-of-rows/

Ruchan
  • 3,124
  • 7
  • 36
  • 72
9

Get response from serverside processing following way it works for me:

// tested with : DataTables 1.10.15
t1 = $('#item-list').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax":'url',
    "drawCallback": function( settings, start, end, max, total, pre ) {  
        console.log(this.fnSettings().json); /* for json response you can use it also*/ 
        alert(this.fnSettings().fnRecordsTotal()); // total number of rows
    },
    ...
});
bharat
  • 1,762
  • 1
  • 24
  • 32
3
$('#jobSearchResultTable').dataTable().rows().count()
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Derek Brown Mar 27 '18 at 04:22
  • @Praveen Please check after filter (search) in table – Satish Nov 07 '19 at 09:28
  • This has at least 2 problems: (1) you have to use `.DataTable()` not `.dataTable()` in order to use `.rows()`, and (2) it returns the FILTERED number of rows, not the total number. – user9645 Mar 22 '21 at 18:04
2

I tried most of the solution from the answers but not worked for me.

Some work but when we search,it shows same number instead number of rows after search.

 var tbl=$('#tbl_name').DataTable();
 console.log(tbl['context'][0]['aiDisplay'].length);

Its worked for me even after search,shows current number of rows after search.

Satish
  • 696
  • 1
  • 11
  • 22
1

According to your description I assume that you have table with id="jobSearchResultTable".

Try this it could work for you.

$(document).ready( function() {
   $('#jobSearchResultTable').dataTable({
    responsive: true,
    "scrollY":  500,
    "scrollCollapse": true,
    "jQueryUI":       true,
    "aaSorting": []

   });
   var count=0;
   $('#jobSearchResultTable tr').each(function(){
      count++;
   });
   alert(count);

)};

This will show the no of rows in the table.

wupendra
  • 76
  • 8
  • but i want the total row count i.e it is showing the current page row count i.e if the table has 50 rows then i want the count as 50 ,not as 10 perpage – lucifer May 20 '15 at 11:06
  • I think this is the correct answer for you (http://stackoverflow.com/questions/3238047/jquery-datatables-row-count-across-pages?lq=1) – wupendra May 20 '15 at 11:41
1

Please check out the following code

var table = $('#example').DataTable(); 
alert(
    'Number of row entries: '+
    table
        .column( 0 )
        .data()
        .length
);
Ali
  • 2,702
  • 3
  • 32
  • 54
Abdul khader
  • 67
  • 13
1

October 2021 for Yajra Datatables

myTable.rows().count();
CodeToLife
  • 3,672
  • 2
  • 41
  • 29
0

//get number of entire rows
table.rows().eq(0).length;

  • This should be a comment, not an answer. I know that you do not have the comment privilege, but you can get enough reputation in a single day... – Badacadabra May 12 '17 at 16:49
0

I just read the table instance in console and got this in version 1.10.8

var table = $("#mytable").DataTable({})
var total = table.settings()[0].json.recordsTotal

Hope this helps

jayson.centeno
  • 835
  • 11
  • 15