Table formatting in export
Table formatting in export
Hello! First of all, DataTables is awesome and we are able to produce some amazing tables for our customers to use. I am using the Buttons extension extensively and I am wondering if there would be any options to support hidden/filtered columns in the table export. I'd like to allow people to search or filter specific rows, hide some columns, then export the data that appears in the table.
If this is not in the plans for datatables, could you point me in the direction of the place to begin in the code and I'll try to implement myself.
Thanks!
This question has an accepted answers - jump to answer
Answers
This already exists. Have a look at this example and the documentation that the example links to.
Allan
Ok, awesome.
I'm looking through the documentation for
pdfHtml5
and pdfMake and trying to see how to preserve table coloration in export as well. Anyone given this a shot?If not, I'll post my code for the customize function when I complete it.
I had access to the table object through a variable named instance.tbl, so this is my customize function:
Cool - thanks for sharing your code with us :-)
Allan
Says instance is not defined in the console, how to solve it then. can someone help me. I just want to change the margin and font size of the table.
instance.tbl
in the above code is the result from$().DataTable()
which jmcshan1 has saved into a variable for his system. You'll need to update the code to match your own.Allan
And to format text in any column?
This is my code, but does not work:
{Extend 'csv' fieldSeparator "" extension '.txt' header: false, text: 'Export'
ExportOptions: {
columns: '3: visIdx'
render: function (data, type, row) {
return data + '1234';
}
}
}
Long that I can not solve.
Best regards
@warmnet2001 - Please link to a test case showing the issue as required in the forum rules.
Edit - The is no
render
function in the export buttons -columns.render
if the function you want. Worth also reading over the orthogonal data manual since the export options provide the ability to read orthogonal data.Allan
I give up, I can not fix it and I can not lose more time. I'll have to find another tool. Thank you too.
Best regards
"The is no render function in the export buttons - columns.render if the function you want".
Can you adjust my example in such a way that I can see how to use
columns.render
, so that I can adjust content of cells before explicitally exporting to Excel?Thanks in advance!
There is an example available in github here. It hasn't been published on this site yet, but it will be soon.
There is an alternative method which you might prefer, but it requires the unreleased version of Buttons. It will be released next week most likely.
Allan
Hey Allan,
Is there a way the exporting option can be modified to apply additional header rows? We are trying to supply additional information at the top of the files, such as the name of the report and the parameters at which it was made from.
chrisShick,
The way I got around it was to create variables prior to initializing the datatable and then passing the variables to the MESSAGE option. EXCEPT: I cannot retrieve the total number of records that are returned in order to display it in the PRINT and PDF features.
Currently no - sorry (unless you use a workaround like that suggested by @seyad96). Currently Buttons will export only one row in the header and footer.
Allan
But, wouldn't the above only work only with the print option?
Allan, is there a function I can override to make this work? It would be great if you can point me in that direction.
If it is all being created into a string and then the file creation, I would love to see if I could just prepend additional rows.
This would be the starting point.
Allan
@chrisShick
I had the same problem and solved it by using a default message, which can be updated at the customize section:
PS: i am setting the column titles manually here, because i used input fields in the table header for filtering.
Hope this helps!
I'm in the same case as dait (input fields in table header for filtering), and i've found i simpler way.
You can just add in the header, just after the input field, a hidden div with column title like this :
Very easy, just one line, it work for all export ;)
PS: here, the use of "display:none" instead of visibility:hidden is better as it will not show a blank space.
Clever trick - nice one. Thanks for suggesting that.
Allan
TypeError: win.content is undefined
$(document).ready(function() {
$('#tblexport').DataTable( {
//"dom": 'T<"clear">rftip',
"paging": false,
"ordering": false,
"info": false,
dom: 'Bfrtip',
buttons: [ {
extend: 'excel',
customize: function (win) {
console.log(win);
});
I am using @jmschan1's excellent code to preserve the table colors on export via Buttons, but I am running into an error:
Uncaught TypeError: Cannot read property '1' of undefined
It's from his code here:
var tblBody = doc.content[1].table.body;
I only have one dataTable in my script, so I am not sure what it is erroring on - I changed the 'instance' variable to reflect the variable I set for my DataTable (oTable).
I guess I'm not understanding what element doc.content[1] is referring to. Can anyone point me in the right direction?
OK, I think I am getting closer - it looks like doc.content is in reference to PDF export, and in my case, I am using the above code for an HTML5 Excel export - I'll take a look at the HTML5 Excel export and see if there is something similar - hopefully it doesn't break the rest of the code...
Yes, for the Excel export you have to modify the XML files that are used.
This example shows a very basic example of that. To insert information you need to have a bit of an understanding of the Open Spreadsheet format.
Allan
Yes, thanks - I am looking into it now - if I figure it out, I'll be sure to post my solution to benefit others!
can i replace header text for each and every column while export to excel , please help me
thanks
@SaiMadhanRockzz - Please do not post duplicates. This is specifically requested in the forum rules.
Allan