Last active
August 29, 2015 14:10
-
-
Save jonasporto/417ceb977e8ff1e915ac to your computer and use it in GitHub Desktop.
SimpleTableToCSV.JS
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
| var TableToCSV = { | |
| tableClass : '.relatorio', | |
| tr : function(){ | |
| return document.querySelectorAll( this.tableClass + ' tr '); | |
| }, | |
| getDataInTable : function(){ | |
| var i = 0, data = this.tr(); | |
| for (; i < data.length; i++){ | |
| var j = 0, header = data[i].querySelectorAll('th'), | |
| body = data[i].querySelectorAll('td'), data_row = ''; | |
| if (header.length > 0 && i === 0) data_row = header; | |
| if (body.length > 0) data_row = body; | |
| for( ; j < data_row.length; j++){ | |
| this.result += "\"" + data_row[j].textContent + "\""; | |
| if (data_row.length -1 != j) this.result += ","; | |
| else this.result += "\n"; | |
| } | |
| } | |
| return this.result; | |
| }, | |
| sendToOpenOrDownload : function(data) { | |
| window.location ='data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(data); | |
| return true; | |
| }, | |
| export : function(tableClass){ | |
| if(tableClass) this.tableClass = tableClass; | |
| return this.sendToOpenOrDownload(this.getDataInTable()); | |
| }, | |
| result : '' | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment