tableexport
Version:
The simple, easy-to-implement library to export HTML tables to xlsx, xls, csv, and txt files
1 lines • 1.08 kB
JavaScript
!function(t){t.fn.table2CSV=function(e){var a=t.extend({},t.fn.table2CSV.defaults,e),n='"'+a.separator+'"',r='"\r\n"';return this.each(function(){var e,o,i=t(this),c=i.find(a.headings?"tr":"tr:has(td)"),s='"'+c.map(function(e,a){var r=t(a),o=r.find("th, td");return o.map(function(e,a){var n=t(a),r=n.text();return r.replace(/"/g,'""')}).get().join(n)}).get().join(r)+'"',f=i.find("caption");switch(a.type){case"csv":default:e="data:text/csv;charset=utf-8,";break;case"txt":console.log(s),s=t.fn.table2CSV.txtFormat(s,a.stripQuotes),e="data:text/plain;charset=utf-8,"}switch(e+=encodeURIComponent(s),a.fileName){case"id":o=i.attr("id");break;case"name":o=i.attr("name");break;default:o=a.fileName}var p='<a href="'+e+'" download="'+o+'" class="export"><button>'+a.buttonContent+"</button></a>";f.length?f.append(p):i.prepend("<caption>"+p+"</caption>")})},t.fn.table2CSV.defaults={separator:",",headings:!0,buttonContent:"Export file",type:"csv",fileName:"export",stripQuotes:!0},t.fn.table2CSV.txtFormat=function(t,e){return e?t.replace(/"(?!")/g,""):t.replace(/""/g,'"')}}(jQuery);