Column Sum Not working ?
Column Sum Not working ?

Hi I want to do sum of some column of my table the code is note working.
Please help me to find out what is wrong in my code.
this is example link
here is my js
<script type="text/javascript">
$(document).ready( function () {
// Setup - add a text input to each footer cell
$('#ContentPlaceHolder1_GridView1 thead tr').clone(true).appendTo( '#ContentPlaceHolder1_GridView1 thead' );
$('#ContentPlaceHolder1_GridView1 thead tr:eq(1) th').each( function (i) {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
$( 'input', this ).on( 'keyup change', function () {
if ( table.column(i).search() !== this.value ) {
table
.column(i)
.search( this.value )
.draw();
}
} );
} );
var table = $('#ContentPlaceHolder1_GridView1').DataTable({
dom: 'Bfrtip',
buttons: [
{
extend: 'print',
title: '',
customize: function ( win ) {
$(win.document.body)
.css( 'font-size', '10pt' )
.prepend( document.getElementById('ContentPlaceHolder1_lblCompanyNameHeader').innerText +
'<div>Freight Contract Register </div><img src="http://datatables.net/media/images/logo-fade.png" style="position:absolute; top:0; left:0;" />'
);
$(win.document.body).find( 'table' )
.addClass( 'compact' )
.css( 'font-size', 'inherit' );
}
},
'excelHtml5',
'csvHtml5',
'pdfHtml5'
],
"paging": false,
"orderCellsTop": true,
});
var sum = $('#ContentPlaceHolder1_GridView1').DataTable().column(12).data().sum();
$($('#ContentPlaceHolder1_lblError')).html(sum);
$('#ContentPlaceHolder1_GridView1').DataTable({
drawCallback: function () {
var api = this.api();
$(api.table().footer()).html(
api.column(12, { page: 'current' }).data().sum()
);
}
});
});
</script>
This question has accepted answers - jump to:
This discussion has been closed.
Answers
To start with Datatables expects you to combine all the options in one Datatable initialization statement. The code you have starting in line 53 should go in the init code you have on line 20. However you probably don't want to use
drawCallback
for this but instead usefooterCallback
. See this example:https://datatables.net/examples/advanced_init/footer_callback.html
HTH,
Kevin
Ok kthorngren its work but what is i want to do for all column
You will need to perform a summation for each column you want to sum. If you don't want to duplicate the code then create a function out of the code that sums and call it for each column.
Kevin
Hi kthorngren when i added "footerCallback" my column search fillters are not working?
all is working but table footer is not going in print
Please see this example:
https://datatables.net/extensions/buttons/examples/html5/footer.html
Kevin