Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables - How can I use my own buttons for exporting?

I'm using Materialize as my UI framework and I'm using DataTables for my tables. I have a table which I need to export to excel and I can do it just fine using the DataTables buttons. However, they are the default DataTables buttons, and I would like to use the Materialize buttons.

So the question is: how can I use my own buttons and have them do the functions of the built-in buttons of DataTables (specifically exporting to Excel)?

I already tried using the option "className" to the Materialize button class, but that didn't work. I figure I probably will have to do some JQuery function.

Thanks everyone!

EDIT: Here's my code:

$(document).ready(function() {
var dataTable = $('#datatable').DataTable( {
    language: {
        search: "",
        searchPlaceholder: "Search"
    },
    dom: 'Brftip',
    buttons: [
        'copy',
        'csv',
        'excel',
        'pdf',
        'print'
    ]
} );
like image 621
Telmo F. Avatar asked Feb 07 '23 21:02

Telmo F.


1 Answers

Use buttons.dom.button to define class and element that would be used for a button.

For example:

var table = $('#example').DataTable({
    "ajax": 'https://api.myjson.com/bins/qgcu',
    "order": [],
    "dom": 'Bfrtip',        
    "buttons": {
       "dom": {
          "button": {
            "tag": "button",
            "className": "waves-effect waves-light btn mrm"
          }
       },
       "buttons": [ 'copyHtml5', 'excelHtml5', 'csvHtml5', 'pdfHtml5' ]   
    }
});

See this jsFiddle for code and demonstration.

like image 86
Gyrocode.com Avatar answered Feb 10 '23 23:02

Gyrocode.com