0

I have some styles that I need to apply to all DataTables buttons so that they match the rest of the buttons on my site. I can add that using the className option as below, but I'd like not to have to supply the same thing every time.

Manual example

$('#myTable').DataTable({
  buttons: [
    {
      text: 'I look like a button',
      className: 'icanhazdefalt'
    }
  ]
})

I see in the docs that the default value is undefined. I couldn't find anywhere in the docs that you could override the default for this or other options. Is this possible? Something like:

$.fn.DataTable.Buttons.options.extend({
  className: 'icanhazdefalt'
})

What I need is to be able to set the default for the plugin itself (rather than for a specific instance). Then all instances I create on the page from then on would have the default I specified. I can include the script that sets the default right after the plugin script (perhaps in a layout file) so that I never have to manually do anything to get all subsequent instances to have the default className (but still be able to override it by explicitly providing it as shown in the 'manual example' above).

4

1 回答 1

0

利用:

$('#myTable').DataTable( {
    buttons: {
        buttons: [
            { extend: 'copy', className: 'copyButton' },
            { extend: 'excel', className: 'excelButton' }
        ]
    }
} );

参考:https ://datatables.net/reference/option/buttons.buttons.className

编辑:可能有一种更好、更简单的方法可以做到这一点,但是,这就是我目前想出的。

//DataTable
var table= $("#myTable").DataTable( {
    dom: 'Bfrtip',
    buttons: [
        {
            text: 'I look like a button'
        },
        {
            text: 'I dont'
        }
    ]
} );

//Add class to all buttons
$(table.buttons()).each(function(){
    $($(this)[0]["node"]).addClass("sampleClass");
});

您还可以通过为按钮()提供参数来更改按钮选择。请参阅此链接

于 2017-01-05T08:47:08.400 回答