Printing/Exporting multiple datatables at once
Printing/Exporting multiple datatables at once
toborix
Posts: 1Questions: 0Answers: 0
Is it possible to print/export multiple datatables at once. So that to have one Print button for all the tables on the page? Thanks!
This discussion has been closed.
Replies
Allan
[code]
"aButtons": [
{
"sExtends": "csv",
"fnClick": function ( nButton, oConfig, oFlash, event ){
/* get data from each table */
event.stopPropagation(); /* does not work */
var tables = document.getElementsByClassName("dataTables_wrapper");
var all_data;
for (var i = 0; i < tables.length; i++)
{
/* get ID of tables[i] and get its dataTable */
var id = tables[i].id;
id = id.substring(0, id.length - 8);
var d_table = $('#' + id).dataTable();
var the_data = d_table.fnGetDataTable(oConfig);
all_data += the_data + "/n"
}
var flash = new ZeroClipboard.Client(); /*.... am I doing this right? */
this.fnSetText( flash, the_data );
/* ... ? */
}
}
]
[/code]
Digging through the code I can't figure out what I need to do next to export the data I gathered - is there a way to do this?
Thanks so much!
[code]
"fnClick": function( nButton, oConfig, flash ) {
this.fnSetText( flash, this.fnGetTableData(oConfig) );
},
[/code]
You want to call fnSetText as well ( http://datatables.net/extras/tabletools/api#fnSetText ).
Allan
Unfortunately I'm still having some trouble. when I call fnSetText, does oConfig need to be unique to each DataTable I'm getting the data from?
Also,
[code]
for (var i = 0; i < tables.length; i++)
{
var id = tables[i].id;
id = id.substring(0, id.length - 8);
var d_table = $('#' + id).dataTable();
all_data = all_data + this.fnGetTableData(oConfig) + " ";
}
this.fnSetText(flash, all_data);
[/code]
is this
[code]
all_data = all_data + this.fnGetTableData(oConfig) + " ";
[/code]
the right approach? Every time I call this with alerts in there, any alerts after that line fail and my spreadsheet always comes out blank (could be because I have my "export all data" button set attached to a blank table...). I don't know how else to combine the data though.
Going to send a donation your way now :) Thanks so much for an awesome project!
To get the data from all tables, what I would suggest is this:
[code]
var s = '';
var a = TableTools.fnGetMasters();
for ( var i=0, iLen=a.length ; i
[code]
var s = '';
var a = TableTools.fnGetMasters();
alert( a.length );
for ( var i=0, iLen=a.length ; i
My current test code looks like this:
[code]
$('#study_table_full').dataTable({
"bPaginate": false,
"bLengthChange": true,
"bFilter": false,
"bSort": true,
"bInfo": false,
"bAutoWidth": true,
"sDom": 'Rlfrtip<"clear spacer">T',
"oTableTools": {
"sSwfPath": "/copy_cvs_xls_pdf.swf",
"aButtons": [
{
"sExtends": "csv",
"fnClick": function( nButton, oConfig, flash ) {
var s = '';
var a = TableTools.fnGetMasters();
alert( a.length );
for ( var i=0, iLen=a.length ; i
[code]
$('table.display').dataTable({
"bPaginate": false,
"bLengthChange": true,
"bFilter": false,
"bSort": true,
"bInfo": false,
"bAutoWidth": true,
"sDom": 'lfrtip<"clear spacer">T',
"oTableTools": {
"aButtons": [
{
"sExtends": "csv",
"fnClick": function( nButton, oConfig, flash ) {
var s = '';
var a = TableTools.fnGetMasters();
for ( var i=0, iLen=a.length ; i
Allan
Allan
In the code you posted above that works, does each of the tables that you want to export to a single file have a class of "display", or just the one you want to attach the "export all" buttons to? Thanks again.
Allan
thanks so much for offering to take a look at the page - the url is
http://srdr-dev.heroku.com/projects/1/studies/1
it shouldn't ask you to log in, but if it does you can use username: admin and password: srdr2011
The button set I am trying to use for all-table export is at the top of the page.
I was thinking, could it be because my tables don't have a consistent format? Will I be able to export all tables still if they don't all have the same headers?
Sarah
Brilliant - thanks for the link. It didn't ask me for a password (you might want to delete it from the forum btw - just incase some nasty person skims it...). Just having a look at the code - it looks like you haven't got the latest TableTools with the fix that I put in. If you grab the latest from here: http://datatables.net/download/build/TableTools.nightly.js - I think that might make all the difference :-). Although of course let me know if not!
Regards,
Allan
and, oops, i had only replaced the one function that you mentioned in my TableTools.js, I hadn't replaced the whole thing. I'm now getting popups in the loop, but the actual save dialog isn't showing up yet.
Now i'm thinking it could be because I added a custom option to the table settings (to include grouping names when exporting, if a column is grouped in the table header). Maybe I messed something up there. I'll take out the option in my table setups and see if I can get the multi-table export going with the nightly build js.
Sarah
I did three things:
- took out the custom option code I wrote (it must have broken something silently.. oops)
- fixed an issue with multiple tables having the same ID
- but it didn't show me the save dialog until I took the alert out of the table loop. that was strange.
but anyway, it works, and it looks great! Thanks so much for all the help, you are a life saver! Now I guess I'll be going back to studying the API so I can write code for tabletools without breaking exporting :) thanks again!
Regards,
Allan
Like Sarah, I try to print and export several tables at once with TableTools.
The tables looks fine (with juste buttons on the top table) but the csv export just create an empty file, and the print occurs an error : "a[i].fnGetTableData is not a function".
[code]
....
"aButtons": [
{
"sExtends": "csv",
"fnClick": function( nButton, oConfig, flash ) {
var s = '';
var a = TableTools.fnGetMasters();
for ( var i=0, iLen=a.length ; i