Search code examples
jquerydatatablestabletools

jQuery DataTables - TableTools. Activate buttons through trigger jQuery?


So I'm using jQuery datatables plugin with tabletools in order to export .csv files. I do not want the buttons to be visible but I still wanted to access these functions through another drop down menu elsewhere on the page.

I have tried hiding the .DTTT_container class in CSS and then firing a click event in jQuery:

$(".DTTT_button_csv").trigger("click");

However it does not save the .csv file.
Is there a way to do this?

Here is my initialising code for dataTable()

    var table = $("#table").dataTable({
        deferRender: true,
        sDom: "<'dt-toolbar'<'col-sm-6 col-xs-12 queryBreadcrumbs'>r<'col-xs-12 col-sm-6'Tf>>" +
            "t" +
            "<'dt-toolbar-footer'i>S",
        oTableTools: {
            aButtons: [
                "csv",
                {
                    sExtends: "pdf",
                    sTitle: "PDF",
                    sPdfMessage: "PDF Export",
                    sPdfSize: "letter"
                }, {
                    sExtends: "print",
                    sMessage: "Generated by ####### <i>(press Esc to close)</i>"
                }
            ],
            sSwfPath: ".../datatables/swf/copy_csv_xls_pdf.swf"
        },
        language: {
            searchPlaceholder: "Search",
            search: "_INPUT_" //no label
        },
        scrollY: scrollY,
        scrollCollapse: false,
        autoWidth: false,
        order: order
    });

Solution

  • Short answer: It's not possible, because buttons "Copy", "CSV", "Excel" and "PDF" are implemented using Adobe Flash.

    See answer to this post from Allan Jardine, author of jQuery DataTables.

    It's not possible I'm afraid. It's a security feature of Flash, so it can't trigger a file save or copy to clipboard without the user knowing it. It would be a huge problem if you could copy to clipboard on just a hover event!

    Alternative solution would be to use jQuery table2excel, see my answer on how to export whole table into Excel for more details. Please note that plug-in may be in early stages of development and has some Excel format issues.