clarketm / TableExport

The simple, easy-to-implement library to export HTML tables to xlsx, xls, csv, and txt files.
https://tableexport.travismclarke.com/
Apache License 2.0
888 stars 289 forks source link

Migration from 3.x to 5.x #108

Closed douglasdtc closed 6 years ago

douglasdtc commented 6 years ago

Hello, When migrating from 3.x to 5.x we can no longer export data simply by calling the tableExport method.

Here's how used in 3.x $("#table").tableExport({ type: 'excel' })

This generates and download a file, usually named as "download.xls".

However, on 5.x, even when setting exportButtons: false there's no way to download it correctly.

What is the correct way to export data as XLSX?

Thank you.

clarketm commented 6 years ago

@douglasdtc – can you try:

$("#table").tableExport({formats:['xlsx']});
// OR
$("#table").tableExport({formats:['xls']});
douglasdtc commented 6 years ago

@clarketm , if I do that, without exportButtons: false it generates the export button, and if I disable the exportButtons, It does nothing.

Here's an example:

https://codepen.io/anon/pen/bLrZBp

Thank you

clarketm commented 6 years ago

@douglasdtc – here is an example where you can work with the export data directly using custom export buttons (i.e. exportButtons: false).

This example has the added benefit of caching the data and saving it to session storage. If you want/need to reinitialize on each click (e.g. dynamic tables) you can just move the getExportData() call inside the handler.

douglasdtc commented 6 years ago

@clarketm Thank you very much for your help. With your example, we've created a function which reproduces the v3 behavior. For us, our selector will always return 1 element, that's why we have Object.keys(exportData)[0], so we can access the first property without knowing it. Here's the final result:

`

function exportTableAsXLSX(selector, settings) {

// Default options
let defaults = {formats: ["xlsx"], exportButtons: false };

// Merge user settings with defaults
let options = $.extend(defaults, settings);  

// Create the TableExport object for further manipulation
let table = $(selector).tableExport(options);

// Get the exportData for the XLS format and store it here so we can access the object properties without knowing them
let exportData = table.getExportData();

// Get the data for export
let {data, mimeType, filename, fileExtension} = exportData[Object.keys(exportData)[0]]['xlsx'];

// Export the file (should fire browser download)
table.export2file(data, mimeType, filename, fileExtension);

}

`

clarketm commented 6 years ago

@douglasdtc – I appreciate the explanatory example outlining your resolution. I am going to close this issue. If any further migration issues arise, don't hesitate to reopen.