Skip to content

Instantly share code, notes, and snippets.

@vividvilla
Created July 17, 2017 19:35
Show Gist options
  • Save vividvilla/43d15b25404504f0aeffc8cdcbca469e to your computer and use it in GitHub Desktop.
Save vividvilla/43d15b25404504f0aeffc8cdcbca469e to your computer and use it in GitHub Desktop.
Datatable JSON export module for Buttons extension
DataTable.ext.buttons.json = {
bom: false,
className: 'buttons-json buttons-jsonhtml5',
available: function () {
return window.FileReader !== undefined && window.Blob;
},
text: function ( dt ) {
return 'JSON';
},
action: function ( e, dt, button, config ) {
this.processing( true );
var data = dt.buttons.exportData();
var JSONData = [];
var keys = data.header;
for (var i=0; i<data.body.length; i++) {
var d = {}
for (var j=0; j<data.body[i].length; j++) {
d[keys[j]] = data.body[i][j];
}
JSONData.push(d);
}
$.fn.dataTable.fileSave(
new Blob([JSON.stringify(JSONData, null, 2)]),
_filename(config)
);
this.processing( false );
},
filename: '*',
extension: '.json',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment