How to add second footer to print in Tfoot?
How to add second footer to print in Tfoot?

Guys, I can't print the second footer in <tfoot> tag?
How can I resolve this issue?
This discussion has been closed.
Guys, I can't print the second footer in <tfoot> tag?
How can I resolve this issue?
Answers
There is no option to print multiple headers or footers using the print button at this time.
If this is a required feature for you, you'd need to modify the source to add that ability.
Allan
allan would you mind if you can tell me, what file contains that functionality?
and Im on in modifying the file
This is where Buttons gets the header information. Likewise the footer follows that.
Currently that is a 1D array. You'll need to update that to get a 2D array of data (i.e. rows and columns) and then update the output functions to accept the 2D array, instead of the 1D they currently expect.
Allan
I've updated lisarush's answer https://datatables.net/forums/discussion/22592/export-multiple-row-headers for the latest version of DataTables. My changes were only made to support multiple headers (for .csv, .xlsx, and copy), but the same logic should easily apply for footers as well. I haven't worked on the code for multiple headers for PDF yet, but will update if I do.
The lines commented out with // are the original source code.
My changes are below:
dataTables.buttons.js:
New Code:
Modified Code:
In _exportData function:
buttons.html5.js:
Modified Code:
In _exportData function:
In DataTable.ext.buttons.excelHtml5 action function:
buttons.print.js:
Modified Code:
In DataTable.ext.buttons.print function:
Very nice indeed - thank you for sharing this with us!
Allan
@dfader, Great work, very helpful for us newbies! Could you please provide an example for footers as well? I have been trying to modify your code to work for a multiple row footer, but I cannot get it to work.
Hi,
I have been able to add multiple headers for pdf. Also a basic merge for colspan/rowspan headers in xlsx. Unfortunately, I was working with rstudio/DT which I am not sure is exactly the same as the datatables.net code or maybe the versions are different. All the same I think it might be portable to datatables even if only in concept. I have posted my steps here: https://github.com/rstudio/DT/issues/418. I hope this helps.
Chris
@dfader thanks for the code, this is working well except for one thing...if I use exportOptions, columns to omit certain columns, the export is still picking up all the columns in the header. Have you experienced this? For instance, I have a simple table that has three columns, but I only wish to export the second and third columns. When I export to Excel, let's say, it's still showing all three column headers, but then exporting data for the second and third columns only, which is throwing off all the rows.
I was able to make a simple modification to get this to work.
In the _exportData function a variable already exists that contains the column indexes that have been selected: config.columns. I'm passing this value into the getHeaders function that you created above, along with the datatable, like so:
In the getHeaders function, where you start to parse through the columns in each row, I'm wrapping the code in this statement:
So now the getHeaders function looks like this:
And that's it!! Hope this helps others who may run into this problem.
Hey @puffster I have a question to further go down this path. So I was experiencing the same issue as far as export taking all the columns even if you set them to not be selected. I added in the code you posted and it works for Copy/Excel(CSV) but Print is not acting how I suspect it should. It appears to print the first row (2 TR in my case) fully without restricting the print like the sorted row does
so instead of the column visibility affecting all the columns/<tr>'s, it seems to not affect the extra <tr> (that doesn't have the sorts on it).
I tried forcing the array's to match one another in length depending on if it was trying to print or not but I couldn't find a way to get it to fire off the click event in that class (dt-buttons), let alone the print specific class.
it looks like it isn't taking in ColSpan's, so even though I have 9 physical TH rows, its spanned over 27 and thus extends out the side while the sort row has 27 columns so it properly removes the columns based on what is hidden on the print button.
Any ideas?
Also ran into an IE Edge/FF issue with Copy and Excel complaing about .length when I used @puffster code to have it properly remove the hidden columns (the array is returning null/unidentified so length on it screws it up?)
@Kergnob sorry for not responding sooner, I was hoping to get some time to try to create your issue for myself, but I haven't. Unfortunately I'm not sure why printing would not behave in the same manner as exporting to Excel does...have you had any luck in solving your issue yet?
In building from my original post (which I'm unable to comment on) and subsequent work/updates here (by dfader) to handle dataTables updates, I've included how I've updated the code to support multi-row headers -- taking into account columns that may be initially hidden (this is where I had the biggest issue as they are not included in the DOM) and any columns that may be marked to exclude from an export (we do this with a class .noExport). The result will export all currently visible columns that are configured via 'config.columns'. Also handles 'messageTop/Bottom' options. "getHeaders" is only slightly changed from the original; it's the loop afterwards that's different. Would probably be more efficient to combine (like puffster did), but I like the logic separation. All code included here for clarity (all original code is still there, just commented out). Hope this helps.
inside buttons _exportData:
in buttons.html5 _exportData:
in DataTable.ext.buttons.excelHtml5 action:
in DataTable.ext.buttons.print action:
Thanks for this and all the effort you put into it. I applied all of the changes but the code seems to stop on
I get the following error:
I tried using an older version of dataTables and the newer but I get the same error on both...
Any idea?
it work for me but i change my _exportData function in buttons.html5.js file like this :
I've created a custom buttons.html5.js (based in lisarush's solution) that allows Multiple Headers and Footers for Excel export:
https://gist.github.com/emersonmoretto/41993309f74a4b09f8e90c0a541de342
@emoreto tu codigo funciona pero, se puede hacer que se combiene las seldas para que sea más facil de saber que grupos pertecen a ese encabezados, ya que uso diferentes encabezados, algo similar a las tablas de excel.
@emoretto Your code works, but can you combine the cells to make it easier to tell which groups belong to those headers, so that the result is similar to the table being exported?
This is the table of my page: as you see the titles are ordered.

This is what the file looks like when exported to Excel - as you can see, the titles are not ordered:

I want the excel file to be similar to the table that was exported or at least the headers are sorted: