I utilized dataTable.view to show particular data in my Django app and I'd like to show buttons such as csv and excel.
I add buttons in options but when I want to construct them (according to the document), this error: $.fn.dataTable.Buttons is not a constructor appears.
I also added buttons.dataTables.min.js , buttons.html5.min.js, buttons.foundation.min.js in the UI.
var options = {
.....
buttons: [
'excel', 'csv'
],
sDom: 'Bfrtip'
}//end of defaultDataTableOptions
var datatable = datatableview.initialize($('.datatable'), options);
var table = $('#DataTables_Table_0').DataTable();
new $.fn.dataTable.Buttons( table, {
buttons: [
'copy', 'excel'
]
} );
table.buttons().container().appendTo( $('#buttons', table.table().container() ) );
$.fn.dataTable.Buttons is not a constructor
I also wasn't able to show export buttons using $.fn.dataTable..
But I was able to find another solution to show export buttons:
This will show the following:
Used cdn's:
Full tested file:
(As Stack Overflow isn't able to let users use a download button, these export buttons will not work in the snippet.)
--EDIT (after @NASRIN's comment):
To export all table data, you would need to add a "all" or "show all" button, so instead of 10-per-page, as you normally would, you would show all table data on the same page, and export. This is because the export will only download the currently shown table data.
See example:
You can do this by using the following:
Source: https://datatables.net/examples/advanced_init/length_menu
More information can be found on this question.