How to trigger Export plugin with Javascript?

How to trigger Export plugin with Javascript?

daveslabdaveslab Posts: 40Questions: 14Answers: 0

Hi folks,

Is there a way to trigger an export of a Datatables instance via Javascript? The only way I can think of is to put the buttons in a hidden div and then trigger a click event.

Replies

  • daveslabdaveslab Posts: 40Questions: 14Answers: 0

    Okay, I get to answer my own question Q&A style.

    You need to use button().trigger() which is documented here.

    All I had to do is change the dom option of the Datatable from this Bfrtip to frtip to hide it and I was still able to trigger the button. With the export buttons setup like this:

    buttons: [
    { extend: 'collection',
      text: 'Export',
      buttons: [
        'copy',
        'excel',
        'csv',
        'pdf'
      ]
    },
    

    I was able to select the export to PDF button with this selector: 0-3

  • allanallan Posts: 57,300Questions: 1Answers: 9,127 Site admin

    Yup - perfect answer :-). Thanks for posting back!

    Worth noting for anyone else reading this, that if you happen to be using the Flash buttons this will not work due to the security restrictions in Flash. It will only work for HTML5 buttons.

    Allan

This discussion has been closed.