How to count records
How to count records
$(document).ready(function() {
$('#example').DataTable( {
"footerCallback": function ( row, data, start, end, display ) {
var api = this.api(), data;
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
total = api
.column( 4 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 4, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 4 ).footer() ).html(
'$'+pageTotal +' ( $'+ total +' total)'
);
}
} );
} );
That code just sums the totals. For an average, divide that total by the number of entries ,How to count records? thinks
This question has an accepted answers - jump to answer
Answers
table.rows().count()
table.rows( {page:'current'} ).count()
https://datatables.net/reference/api/count()
https://datatables.net/reference/api/rows()
Yep, as @rf1234 said, use those for the table totals. For the total records on the page, use the
selector-modifier
forrows()
, so something liketable.rows({page:'current'}).count()
,Colin
Thanks. The number of records can be obtained by summing. when the filter conditions are changed, total3 and total2 can automatically change according to the filter conditions, but pcount(the number of records) will not change. What is the reason?
I don't think it's doing what you think it is. Line 56 is just always returning the number of rows in the table, and nothing else. What are you expecting it to do?
Colin
When the table filter changes, its value can change at the same time,Just like the other two values
Now its value is fixed. When there are fewer filtering records in my table, it still displays the total number of records in the table. I want to display the number of records after filtering
totals 2 and 3 have "search: applied".
pcount does not have this in your code.
it's all in the docs ...
Please read: https://datatables.net/reference/type/selector-modifier
Yes, if I change it to search: applied, clicking the next page will be slow, and the returned value is also the total number of records, not after filtering
I have no idea why you are coding it that way. My code looks different. And you still are not applying "search" to table.rows().count().
Thanks ,I was wrong