TableTools CSV/PDF/XLS/Print export onclick functionality

TableTools CSV/PDF/XLS/Print export onclick functionality

jLinuxjLinux Posts: 981Questions: 73Answers: 75

I was wondering if theres a way to add some functionality via JS for when the TableTools buttons are clicked, specifically the export buttons.

I tried initiating the table like so...

var tableTools1 = new $.fn.dataTable.TableTools( table, {
            "sSwfPath": "/include/plugins/DataTables-1.10.7/extensions/TableTools/swf/copy_csv_xls_pdf.swf",
            "sRowSelect": "multi",
            "aButtons": [
                {
                    "sExtends":    "csv",
                    "bSelectedOnly": true,
                    "sButtonText": 'CSV',
                    "fnInit": function ( nButton, oConfig ) {
                        $(nButton).addClass('btn btn-xs btn-primary p-5 export-csv');
                    }
                }
]
});

Then using the JS code:

$('body').on( 'click', '.export-csv', function (e){ alert('CSV Export Clicked'); });

But that doesnt seem to work

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    I was able to get this working via fnClick

    Thanks!

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited August 2015

    I seem to be having an issue with it though.. When I use the following code:

    {
        "sExtends":    "pdf",
        "bSelectedOnly": true,
        "sButtonText": 'PDF',
        "fnInit": function ( nButton, oConfig ) {
        $(nButton).addClass('btn btn-xs btn-primary p-5 export-pdf');
        },
        "fnClick": function ( nButton, oConfig, oFlash ) {
            console.log('Export..');
        }
    }
    

    It logs the console message, but it wont export the PDF, but when I remove the fnClick, like so:

    {
        "sExtends":    "pdf",
        "bSelectedOnly": true,
        "sButtonText": 'PDF',
        "fnInit": function ( nButton, oConfig ) {
        $(nButton).addClass('btn btn-xs btn-primary p-5 export-pdf');
        }
    }
    

    It exports just fine.

    Whats weird, is if I put the fnClick with the console.log() in the CSV or XLS export buttons, it will work just fine (Export and console.log work), but when I put the exact same fnClick code in the PDF or Print buttons, it fails

    Anyone run into this ever?

    I found a similar issue here: http://datatables.net/forums/discussion/8899/fnclick-not-working-in-tabletools-button-defintion However the link posted in there for assistance now leads to a 404 >.<

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Answer ✓

    Hi,

    What you need to do is use fnClick as you have found, but just setting your own fnClick will override the default option. So if you want to retain the default action you would need to also copy the code from the source file and paste it into your action.

    A bit messy I know, but that's how it works...

    Out of interest, do you remove the classes after a set amount of time?

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Well my goal is to see who exports what and in what format, so when its clicked, an ajax call is made to log the data, then the class is removed to prevent duplicate logging

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    I see - thanks. It just piqued my curiosity :-)

    Allan

This discussion has been closed.