Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the entire number of rows in a datatable

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

like image 209
lucifer Avatar asked May 20 '15 10:05

lucifer


2 Answers

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()

like image 129
jpaik Avatar answered Oct 14 '22 06:10

jpaik


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/

like image 28
Ruchan Avatar answered Oct 14 '22 05:10

Ruchan