Extract excel error: undefined (reading 'length') from footer
Extract excel error: undefined (reading 'length') from footer
imneckro
Posts: 4Questions: 1Answers: 0
WHen I'm trying to extra excel, footer throws error:
buttons.html5.min.js:8 Uncaught TypeError: Cannot read properties of undefined (reading 'length')
at buttons.html5.min.js:8:21584
at _Api.action (buttons.html5.min.js:8:21857)
at run (dataTables.buttons.js:807:19)
at dataTables.buttons.js:822:7
If remove footer excel exports fine.
Here is my code:
$('#getvatinfo').dataTable({
dataSrc: '',
bPaginate: false,
"info": false,
"paging": false,
layout: {
topStart: {
buttons: ['copy', 'csv', 'excel', 'pdf', 'print']
}
},
columnDefs: [{
target: 1,
render: function(data) {
return moment(data).format('DD/MM/YYYY');
}
},
{
targets: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42],
render: DataTable.render.number(null, null, 2, '')
}
],
"language": {
"search": "Αναζήτηση"
},
"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(/[\$a-zA-Z, ]/g, '') * 1 :
typeof i === 'number' ?
i : 0;
};
var cols = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42]; // your columns position will come in this array
for (let index = 0; index < cols.length; index++) {
var col_data = cols[index];
// Total over all pages
total = api
.column(col_data)
.data()
.reduce(function(a, b) {
return intVal(a) + intVal(b);
}, 0);
// Total over this page
pageTotal = api
.column(col_data, {
page: 'current'
})
.data()
.reduce(function(a, b) {
return intVal(a) + intVal(b);
}, 0);
// Update footer
$(api.column(col_data).footer()).html(
pageTotal.toLocaleString('el-GR', {
minimumFractionDigits: 2
})
);
}
},
initComplete: function(settings, json) {
$("#perfrom").text(datefrom);
$("#perto").text(dateto);
fillper();
}
});
Answers
Exporting seems to work fine with two footer rows in this test case:
https://live.datatables.net/qexunane/1/edit
Possibly the problem is with the structure of your footer. Please provide a link to your page or test case replicating the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Here is my table:
https://justpaste.it/ewfn2
I copied your table into this updated test case and it still works.
https://live.datatables.net/yequpene/1/edit
Please provide a link to your page or build a test case that replaictes the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Looks like the issue might be with using the
footerCallback
to update the footer. I used this example and it throws the following error when exporting Excel:Here is the test case:
https://live.datatables.net/qanageto/1/edit
@allan will need to take a look.
Kevin
I just remembered the issue in this thread. You are running into the same issue which has a fix. The next release of buttons.html5.js will have the fix. Or you can download the buttons.html5.js nightly from here which has the fix then pickup the next version once released.
Here is the updated test case with buttons.html5.js nightly and it exports with no errors:
https://live.datatables.net/sulehuso/1/edit
Kevin
Kevin thanks a lot for your help.
With nightly version error no longer occurs.
Exported excel footer sums though are empty.
I noticed that too. I started this thread. There is a fix in the nightly code or you can wait until the official release of the next versions. Maybe next week.
Kevin
I'm using https://nightly.datatables.net/buttons/js/buttons.html5.js but still footer empty.
Allan stated this:
The fix was placed in two places. So you will probably also need the nightly datatables.js.
Kevin