Printing/Exporting multiple datatables at once

Printing/Exporting multiple datatables at once

toborixtoborix Posts: 1Questions: 0Answers: 0
edited March 2011 in TableTools
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!

Replies

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    There isn't a built in way to do it, but you could potentially use the API to do it. You could override fnClick for your buttons for example and then get the data from each of the tables and pass that over to flash for saving. Might require a little reading of the TableTools code :-)

    Allan
  • sarahsarah Posts: 17Questions: 0Answers: 0
    I'm trying to do this at the moment... I'm stuck on doing the file creation part, and also preventing the original button event.

    [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!
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Don't create a new ZeroClipboard instance - just use the one which TableTools has already created. For example in the default copy fnClick function there is:

    [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
  • sarahsarah Posts: 17Questions: 0Answers: 0
    Thanks so much for the response!

    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!
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    oConfig is the object that contains the fnClick function, and all the other properties of the button (slightly confusing circular reference :-) ). It just allows the various functions to get all the properties which have been set. So it should be unique for where ever you want different code, and just use the same for when they can be the same.

    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
  • sarahsarah Posts: 17Questions: 0Answers: 0
    hmm, I'm still not having any luck with this. I'm currently attaching my button with my custom fnClick function to an empty DataTable. When I put a javascript alert in the loop, it's only called once - but printing a.length shows that it is 8. Would attaching my "export all" link/click function to an empty table be the cause of problems like this?
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    This in fnClick gives you an alert of 8 and then only 1 alert in the loop?!:

    [code]
    var s = '';
    var a = TableTools.fnGetMasters();
    alert( a.length );
    for ( var i=0, iLen=a.length ; i
  • sarahsarah Posts: 17Questions: 0Answers: 0
    Unfortunately yes... using that exact code (same as what I put in before, copy-pasted just to be sure) I get an alert of "8" (or however many tables total are in the page), then the save dialog appears, then an alert of "0", then nothing.

    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
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Okay I've just tried it and the following code works okay for me:

    [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
  • sarahsarah Posts: 17Questions: 0Answers: 0
    I do have DataTables and TableTools initializing on each other table in the page. I think I'm attaching the TableTools code to the wrong place. I was trying to attach it to a single blank table so the "export all" buttons would show up underneath that blank table, and still go through all other tables in the page. Do I need to attach the code to table.display to have all of the data be included? Where would the TableTools buttons show up in that case? (using table.display gives me no buttons, except the ones already initialized on existing tables...)
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    You could use that initialisation on the first table. And then use an empty aButtons array for TableTools on all the other tables. That should do the job.

    Allan
  • sarahsarah Posts: 17Questions: 0Answers: 0
    Sorry, I'm still a little confused. Does that mean it's not possible to have a TableTools button set to export all the tables in a page in one document, and also have a button set to export each individual table separately?
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    I see - sorry I misunderstood. Yes that is possible. Do you want all tables to have both buttons? If so - then initialisation like I've got above, plus just a regular 'csv' button should do it.

    Allan
  • sarahsarah Posts: 17Questions: 0Answers: 0
    Hmm.... When I use your code, with the only difference being that I changed the jQuery selector to the ID of my table, I get the same result as I did before (only one alert in the loop, if I put an alert in there). I have all my other tables defined with separate DataTables calls and their own individual IDs, used for the jQuery selectors. Should I be defining all the DataTables with the same selector in order to export all the tables to 1 file?

    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.
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Every table on the page has been initialised with DataTables / TableTools with the above code should have it's data read, regardless of the original selector. Can you give us a link to your page please so I might be able to see where it is going wrong?

    Allan
  • sarahsarah Posts: 17Questions: 0Answers: 0
    Hi,
    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
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Hi 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
  • sarahsarah Posts: 17Questions: 0Answers: 0
    it's just a test site so the password will be changed in the future :)

    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
  • sarahsarah Posts: 17Questions: 0Answers: 0
    !!! It works!!

    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!
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Hooray! Good to hear that did the job :-)

    Regards,
    Allan
  • damsdams Posts: 1Questions: 0Answers: 0
    edited June 2011
    Hi,
    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
This discussion has been closed.