Datatable PDF export footer overall total error?

Datatable PDF export footer overall total error?

abdul_rahman85abdul_rahman85 Posts: 2Questions: 1Answers: 0

Hi,
I have issues when I export PDF or CSV or Excel in particular column overall total want to print in bottom of the table in export document,
1. In webpage is showing page wise is perfect.
2. when i export the file it print only first page total in downloaded document. i need what are the records printed for that record total print in the bottom, Data table not calculate the pagination totals.
Any one plz help me to solve the issues.

I pasted my code below:

$('#tableWithSearch_hourstable').dataTable({
responsive: true,
processing: true,
columnDefs: [{
targets: "no-sort",
orderable: false,
searchable: false,
}],
language: {
paginate: {
next: '»',
previous: '«'
}
},
dom: 'Bfrtip',
buttons: [
{
extend: 'collection',
text: 'Export',
buttons: [
{
extend: 'csvHtml5',
exportOptions: {
columns: [0,1,2,3,4,5,6,7,8,9,10]
},
title: 'ICC Request',
footer: true,
customize: function (csv) {
return "Date:"+date_ref+"\n\n"+ csv;
},
},
{
extend: 'pdfHtml5',
pageSize: 'LEGAL',
messageTop: 'Date: '+curr_date+'-'+curr_month+'-'+curr_year,
exportOptions: {
columns: [0,1,2,3,4,5,6,7,8,9,10]
},
customize: function (doc){
doc.defaultStyle.alignment = 'left';
doc.styles.tableHeader.alignment = 'center';
doc.pageMargins = [ 30, 20, 20, 20 ];
},
title: 'ICC Request',
footer: true,
}
]
}
],
"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( colRef )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this column 8
colTotal_8 = api
.column( colRef, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
$( api.column( colRef ).footer() ).html(
//pageTotal +' ('+ total +' total)'
"$ " + colTotal_8.toFixed(2)
);
}
});

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Please link to a test case showing the issue.

    Allan

  • abdul_rahman85abdul_rahman85 Posts: 2Questions: 1Answers: 0

    Hi Allan,
    I have update my code in JS fiddle,
    https://jsfiddle.net/abdulrahman_85/xt3y2xbd/1/

    for my server pdf and csv is export, but here not,
    My Query:
    when i export the pdf footer toal is taken from first page not overall total or serach record total, (footer i need column value total)

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    when i export the pdf footer toal is taken from first page not overall total or serach record total,

    Ah - I understand now. Thank you for the clarification. To rephrase it, the problem is that you are calculating the total on a per page basis, but the print view shows the count for the visible page only.

    That's because Buttons is just reading the value from the footer - it isn't attempting to do any new calculations.

    What I think you would need to do is use the customize method of the two button types to modify the data being exported, doing the recalculation for the footer in the process. There is no built in option to have it do that recalculation for you.

    Allan

This discussion has been closed.