Hello,
I have a problem with a sum column. The result isn't exactly. I have 321.84999999999997 but it's 321.85.
I tried differents js:
1st solution:
$(document).ready(function () {
var table = $('#summary').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 hours over this page
thisPageTotal = api
.column( 13, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 13 ).footer() ).html(
thisPageTotal + ' heure(s) sur cette page'
{# ( sur '+ allPagesTotal +' heure(s) totale(s))'#}
);
}
});
2nd solution:
$.fn.dataTable.Api.register('column().data().sum()', function () {
return this.reduce(function (a, b) {
var x = parseFloat(a) || 0;
var y = parseFloat(b) || 0;
return x + y;
});
});
$(document).ready(function () {
var table = $('#summary').DataTable({
$('<button>Cliquez pour avoir la somme des coûts de M.O. de la sélection</button>')
.prependTo('#summary')
.on('click', function () {
alert('Column sum is: ' + table.column(13).data().sum());
});
});
3rd solution:
jQuery.fn.dataTable.Api.register('sum()', function ( ) {
return this.flatten().reduce(function (a, b) {
if (typeof a === 'string') {
a = a.replace(/[^\d.-]/g, '') * 1;
}
if (typeof b === 'string') {
b = b.replace(/[^\d.-]/g, '') * 1;
}
return a + b;
}, 0);
});
$(document).ready(function () {
var table = $('#summary').DataTable({
$('<button>Cliquez pour avoir la somme des coûts de M.O. de la sélection</button>')
.prependTo('#summary')
.on('click', function () {
alert('Column sum is: ' + table.column(13).data().sum());
});
});
An idea?