1

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.

5
  • Are you not able to see the buttons on the page? Commented May 7, 2016 at 22:39
  • Issue found , firstly I need the following JS : cdn.datatables.net/buttons/1.1.2/js/buttons.flash.min.js (I'm on Mac , might not be needed on other OS). I also added cdn.datatables.net/buttons/1.1.2/css/buttons.dataTables.min.css for better looking styling. The real issue was some Coffee script issue , once I saw it worked on a different table and I ruled out all real issues I simply replaced the two tables ID and it worked. Commented May 10, 2016 at 14:50
  • So I copied the second table initialization , changed the ID to the other table and it worked fine. +1 new Coffee script hater in the world :) Commented May 10, 2016 at 14:51
  • @KumarAbinash , anyway to flush such issues? I get an error if the indentation is wrong , but for smaller things , any trick I can use? I'm using gvim and Aptana Studio 3 if it helps. Commented May 10, 2016 at 14:55
  • I use myself & there is a linter for coffeescript, which tells you what's wrong while saving the file itself. I use it and it saves me a lot of time. here is the link github.com/kchmck/vim-coffee-script . And for Aptana Studio I guess there's got to some kind of a linter for coffeescript. Try searching. Commented May 10, 2016 at 15:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Didn't help :( I tried also to include all the JS/CSS from the Datatables example and it didn't help. Is it possible my jquery-rails part is missing something?
Issue found , not resolved yet. I think I have some dangling table related tag that caused the issue since I seem to be able to see the buttons in other tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.