Created
July 17, 2017 19:35
-
-
Save vividvilla/43d15b25404504f0aeffc8cdcbca469e to your computer and use it in GitHub Desktop.
Datatable JSON export module for Buttons extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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