Reputation: 2327
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
Upvotes: 15
Views: 131660
Reputation: 1157
On the official API documentation page for DataTables library it is shown that since the version 1.10.8 of DataTables the way to get total count of rows in the table is table.rows().count()
.
Here is a screenshot from the related count()
API page (taken in April 2024):
So for your specific case it should be like this:
$('#jobSearchResultTable').dataTable().rows().count();
Upvotes: 4
Reputation: 3194
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/
Upvotes: 17
Reputation: 726
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.
Upvotes: 2
Reputation: 1776
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
},
...
});
Upvotes: 9
Reputation: 67
Please check out the following code
var table = $('#example').DataTable();
alert(
'Number of row entries: '+
table
.column( 0 )
.data()
.length
);
Upvotes: 1
Reputation: 835
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
Upvotes: 0
Reputation: 311
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()
Upvotes: 31
Reputation: 86
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.
Upvotes: 1