Buttons setting doesn't work

Buttons setting doesn't work

webmazzwebmazz Posts: 2Questions: 1Answers: 0

I wanted to add the ability to export data from table to csv file so:

I add this libraries to my project:
- https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.min.js
- https://cdn.datatables.net/buttons/1.6.5/js/buttons.flash.min.js
- https://cdn.datatables.net/buttons/1.6.5/js/buttons.html5.min.js
- https://cdn.datatables.net/buttons/1.6.5/js/buttons.print.min.js

I update my js file with this:

"dom": "Bfrtip",
"buttons": ["csv"]

Csv export works but I see other buttons also (Copy, Print). How can I turn them off? I will also add that changing the button options (e.g. text ) also does not work. It looks like buttons settings are not being applied.

Here is snippet with my case: https://jsfiddle.net/x8a52fhc/

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited December 2020 Answer ✓

    That took a little bit to track down :smile:

    You are using an HTML 5 data attribute on the table to define the buttons:

    <table class="table table-striped table-datatable" data-buttons="csv">
    

    You code is then getting this value to assign the buttons using dataTableOptions["buttons"] = ["csv"];. The problem is that Datables can directly use HTML 5 data attributes for configuration as described here. Looks like the HTML 5 data attributes take precedence over what is applied using table.DataTable({ .. }). the data attribute is just a string which is not valid for the buttons so it applies all available options.

    I updated your example to show this:
    https://jsfiddle.net/pov0xr82/

    It has this:

    <table class="table table-striped table-datatable" data-buttons='["csv"]'>
    

    And inits Datatables like this:

               table.DataTable({
                 dom: "Brftip"
               });
    

    Only the CSV button appears.

    Kevin

  • webmazzwebmazz Posts: 2Questions: 1Answers: 0

    Everything works now.

    I would never have figured it out.
    Thank you very much!

This discussion has been closed.