Multiple Header Rows - pdfMake
Multiple Header Rows - pdfMake
I've seen a few posts about not being able to export multiple header rows in pdf and not being able to use colspan, so just wanted to share my fix for being able to do both. A simple modification of the buttons.html5.js file should do the trick (though I've only tested on v1.3.3.
In the action function inside pdfHtml5 extension on line 1317 change the following lines
if ( config.header ) {
rows.push( $.map( data.header, function ( d ) {
return {
text: typeof d === 'string' ? d : d+'',
style: 'tableHeader'
};
} ) );
}
to
if (config.header) {
$.map(data.headerStructure, function (hr) {
var a = $.map(hr, function (d) {
var h = d !== null ? {
text: (typeof d.title === 'string' ? d.title : d.title + ''),
style: 'tableHeader',
colSpan: ((d.colspan === null || d.colspan === undefined) ? 1 : d.colspan)
} : '';
return h;
});
rows.push(a);
});
}
This will allow colspans and multiple header rows.
Replies
Hi,
Buttons supports multi-row headers and footers with colspan / rowspan attributes for the cells. See this example.
I can't recall what version that was introduced in, but you are right that it won't have been in 1.3.x. Buttons 3.2.6 is the current release and I would encourage you to update to a supported version.
Allan
I have the datatables.buttons-3.2.3.js file installed. This file I'm referencing is for exporting the DataTable to a file using pdfMake. So there are 3 files involved:
The export one is the one that was modified to be able to export all of the headers to the pdf file instead of the single header row it does by default, and to also allow colspan instead of printing the header cell value in each column spanned (the default).
Looking at the source, the internal name is FileSaver.js (1.3.3).
Hi,
Thanks for your reply and the extra information. I'm not quite understanding though - the example I linked to shows multirow headers being exported by default.
Can you give me a link to a page show the issue so I can take more of a look into it. Certainly my intention is that it should support complex headers by default.
Allan
Looking at the code for the 3.2.6 version of the pdf export in buttons.html5.js from the builder
the code in the file is exactly the same as the code I changed (using the headerStructure object instead of header object). Which is interesting as the version number at the top of the file is still the same (1.3.3) though I'm certain the file has changed as the date still says 2016.
The files I was using were part of the AdminLTE3 bundle, and I updated piece by piece based on the version number and didn't update the buttons export file since the version number at the top was still the same. So you are correct, the header export does work as expected now. Guess that's why there hasn't been any comments about it in a few years.
The reference to the FileSaver v1.3.3 is just part of the
buttons.html5.jsfile, and yes, that hadn't changed for a while (I have actually updated that aspect in a dev build recently). FileSaver is bundled intobuttons.html5.js, but is not all that is in that file.Glad we got to the bottom of it, it is working for you now, and working as expected with the current release.
Regards,
Allan