Reputation: 261
I have a amount column and have used this https://datatables.net/examples/advanced_init/footer_callback.html to calculate the total for on page and off page into the footer.
There is also this example which alerts the total amount on page and off page. https://datatables.net/examples/plug-ins/api.html
What i am trying to achieve is how to sum the values of selected rows of the salary columns
Here is one I built on jsfiddle with the rows selection done and a footer total done.
But I can't figure out how I can add a sum value of the total of selected rows in the bottom left hand corner cell
http://jsfiddle.net/ebRXw/190/
$('#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);
} );
// 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)'
);
}
} );
$('#example tbody').on('click', 'tr', function () {
$(this).toggleClass('selected');
} );
Upvotes: 0
Views: 9375
Reputation: 4637
Try this Working Demo
$('#example tbody').on('click', 'tr', function () {
$(this).toggleClass('selected');
$('#button').trigger('click');
console.log($('tfoot tr > th').eq(1).html( '$'+ $('#selectedtotal').html()));
});
Upvotes: 1
Reputation: 3384
You could check if a row has been selected or not by $(this)[0].classList.contains("selected")
. Then add the amount of the 5th column of the row $(this).find(':nth-child(5)')
to the amount of the $('sum')
:
if($(this)[0].classList.contains("selected"))
{
$('#sum').html(
parseFloat($('#sum').html()) +
parseFloat($(this).find(':nth-child(5)').html().replace(/[$,]/g, "")));
}
else
{
$('#sum').html(
parseFloat($('#sum').html()) -
parseFloat($(this).find(':nth-child(5)').html().replace(/[$,]/g, "")));
}
Here's a DEMO
Upvotes: 0
Reputation: 1377
Add a class of .sum
in the th
element that shows the total sum. Also, create a sum
variable and initialize it at the top of your javascript code.
Then, change your code that toggles the selected
class to this:
$('#example tbody').on('click', 'tr', function () {
var price = parseInt(($(this).find('td').last().html()).replace(/\$|,/g, ''));
if($(this).hasClass('selected')) {
sum -= price;
} else {
sum += price;
}
$('.sum').html(sum);
$(this).toggleClass('selected');
} );
And here is a working JFiddle
Upvotes: 2
Reputation: 10198
var totalSUM = 0;
$("tbody tr.selected").each(function () {
var getValue = $(this).find("td:eq(4)").html().replace("$", "");
var filteresValue = getValue.replace(/\,/g, '');
totalSUM += Number(filteresValue)
console.log(filteresValue);
});
alert(totalSUM);
Upvotes: 0