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

梦想与她 提交于 2019-12-14 01:28:10

问题


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'
    ]
} );

回答1:


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.




回答2:


https://datatables.net/extensions/buttons/custom

buttons: [
    {
        text: 'Reload',
        action: function ( e, dt, node, config ) {
                dt.ajax.reload();
            }
        }
    ]

"text" can be the html to your custom button




回答3:


You should use Jquery to select the button and add the class in the drawcallback. Something like this:

$('.buttons-excel').addClass('waves-effect waves-light btn');

Or:

 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' ]   
   }
 });


来源:https://stackoverflow.com/questions/37595323/datatables-how-can-i-use-my-own-buttons-for-exporting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!