diff --git a/buttons/button.download.js b/buttons/button.download.js new file mode 100644 index 0000000..7d3534e --- /dev/null +++ b/buttons/button.download.js @@ -0,0 +1,113 @@ +/*! Download button for Buttons + * 2018 SpryMedia Ltd - MIT licensed - datatables.net/license + */ + +/** + * @summary Download Button + * @author SpryMedia Ltd (www.datatables.net) + * @copyright Copyright 2018 SpryMedia Ltd. + * + * License MIT - http://datatables.net/license/mit + * + * A button that can be used to trigger a server-side action, typically a + * download of a file generated by the server. If server-side processing + * is enabled in the host table it will automatically add the last parameters + * used for a table draw allowing the script to output a file with the same + * order / search applied as the main table. + * + * This is particularly useful when using server-side processing and wishing + * to allow user export of the data in a table. The default Buttons package + * will only export the data available on the client-side, while this button + * can be used to let the server generate the required file and then download + * it to the client-side. + * + * @example + * // Download button + * var table = $('#example').DataTable({ + * dom: 'Bfrtip', + * buttons: [ + * { + * extend: 'download', + * url: '/api/download' + * } + * ] + * }); + */ +(function(window, document, $, undefined) { + function flattenJson(data, name, flattened) { + if (!flattened) { + flattened = {}; + } + + if (!name) { + name = ''; + } + + if ($.isPlainObject(data)) { + $.each(data, function(idx, val) { + if (name === '') { + flattenJson(val, idx, flattened); + } else { + flattenJson(val, name + '[' + idx + ']', flattened); + } + }); + } else { + flattened[name] = data; + } + + return flattened; + } + + $.fn.dataTable.ext.buttons.download = { + text: 'Download', + action: function(e, dt, node, config) { + // Gather information to be submitted + var data = {}; + + if (dt.page.info().serverSide) { + $.extend(data, dt.ajax.params()); + } + + if (typeof config.data === 'function') { + config.data(data); + } else if (typeof config.data === 'object') { + $.extend(data, config.data); + } + + // Convert data to a flat structure for submission + var flattened = flattenJson(data); + + // Create an iframe + var iframe = $('