I've been having issues trying to add an excel export button when working in React. I assume it has something to do with an import but I can't find much help online related to React and DataTables.net in this regard. I just want a user to be able to download to excel.
these are my imports related to jquery and datatables
const $ = require('jquery');
$.DataTable = require('datatables.net');
import 'datatables.net-dt/css/jquery.dataTables.css'
here is where the table is defined. It works except for the buttons
componentDidMount() {
this.$el = $(this.el);
this.$el.DataTable({
dom: 'Bfrtip',
data: this.makeArray(),
columns: this.getColumns(),
pageLength:this.props.json.length,
buttons: [
{
extend: 'excel',
text: 'Save current page',
fileName: "data.xlsx",
exportOptions: {
modifier: {
page: 'current'
}
}
}]});
}
this is the render method
render() {
return (
<div>
<table className="display" width="100%" ref={el=>this.el=el} />
</div>);
}
any help is appreciated
answered my own question
needed to do install jzip, require it and then attach it to the window object
const jzip = require( 'jzip');
window.JSZip = jzip;
I also switched the button to excelhtml5. Here are all the jquery/datatable imports I have
const $ = require('jquery');
$.DataTable = require('datatables.net');
import 'datatables.net-dt/css/jquery.dataTables.css'
require( 'datatables.net-buttons/js/dataTables.buttons.min' );
const jzip = require( 'jzip');
require( 'datatables.net-buttons/js/buttons.html5.min' );
import 'datatables.net-buttons-dt/css/buttons.dataTables.css'
window.JSZip = jzip;
here is the updated buttons portion of the componentDidMount method.
componentDidMount() {
this.$el = $(this.el);
this.$el.DataTable({
dom: 'Bfrtip',
data: this.makeArray(),
columns: this.getColumns(),
paging:false,
buttons: [
'excelHtml5'
]
}
);
}
Following the bvmcode answer I've been able to solve the problem, but instead of use requires I'm only using imports. To know what imports and components to install: https://datatables.net/download/
My imports:
import * as jzip from 'jszip';
import 'pdfmake';
import 'datatables.net-dt';
import 'datatables.net-buttons-dt';
import 'datatables.net-buttons/js/buttons.html5.js';
import 'datatables.net-fixedheader-dt';
import 'datatables.net-rowgroup-dt';
window.JSZip = jzip;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.