How to use buttons with jquery-datatables-rails?

1.9k Views Asked by At

I'm trying to add buttons to my DataTable. As instructed in https://github.com/rweng/jquery-datatables-rails I added the dataTables.buttons JS.

My application.js:

//= require jquery
//= require jquery.turbolinks
//= require jquery-ui
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require dataTables/extras/dataTables.buttons
//= require bootstrap
//= require turbolinks
//= require_tree .

I didn't find a CSS in the assets of the gem https://github.com/rweng/jquery-datatables-rails/tree/master/app/assets/stylesheets/dataTables/extras.

Hence my application.css (doesn't contain anything related to buttons) :

*= require jquery-ui
*= require dataTables/src/demo_table_jui
*= require_tree .
*= require_self
*/

In my coffee script I have (mimicking https://datatables.net/extensions/buttons/examples/initialisation/export.html ):

jQuery ->
    $('#rating').dataTable(
            bJQueryUI:       true,
            sPaginationType: "full_numbers",
            iDisplayLength:  100,
            scrollY:         "1000px",
            scrollCollapse:  true,
            dom: 'Bfrtip',
            buttons: [
                'copy', 'excel', 'pdf'
            ]
            )

I'm guessing that I'm missing something silly , like the CSS or something colliding options. When I tried adding javascript_tag ( https://cdn.datatables.net/buttons/1.1.2/js/dataTables.buttons.min.js ) / stylesheet_tag ( https://cdn.datatables.net/buttons/1.1.2/css/buttons.dataTables.min.css ) to the view but that didn't help.

1

There are 1 best solutions below

2
Kumar On

Seems you have created the buttons but you still need to show them in the page.

Can you try something like this:

jQuery ->
    table = $("#rating").dataTable(
        bJQueryUI: true,
        sPaginationType: "full_numbers",
        iDisplayLength: 100,
        scrollY: "1000px",
        scrollCollapse: true,
        dom: "Bfrtip",
        buttons: [
            "copy",
            "excel",
            "pdf"
        ]
    )
    table.buttons()
        .container()
        .appendTo( $(".your-div-for-button", table.table().container() ) );

That is, after creating, you append buttons to the body explicitly. Let me know.