forked from hhurz/tableExport.jquery.plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableExport.min.js
27 lines (27 loc) · 13.5 KB
/
tableExport.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(function(g){g.fn.extend({tableExport:function(t){function u(l,n,d,e){-1==a.ignoreRow.indexOf(d)&&g(l).filter(function(a){return g(this).is(":visible")||"always"==g(this).data("tableexport-display")||"always"==g(this).closest("table").data("tableexport-display")}).find(n).each(function(l){if(("always"==g(this).data("tableexport-display")||"none"!=g(this).css("display")&&"hidden"!=g(this).css("visibility")&&"none"!=g(this).data("tableexport-display"))&&-1==a.ignoreColumn.indexOf(l)&&"function"===typeof e){var n=
0;if("undefined"!=typeof y[d]&&0<y[d].length)for(c=0;c<=l;c++)"undefined"!=typeof y[d][c]&&(e(null,d,c),delete y[d][c],l++);e(this,d,l);if(g(this).is("[colspan]"))for(n=g(this).attr("colspan"),c=0;c<n-1;c++)e(null,d,l+c);if(g(this).is("[rowspan]")){var b=parseInt(g(this).attr("rowspan"));for(r=1;r<b;r++)for("undefined"==typeof y[d+r]&&(y[d+r]=[]),y[d+r][l]="",c=1;c<n;c++)y[d+r][l+c]=""}}})}function I(l){!0===a.consoleLog&&console.log(l.output());if("string"==a.outputMode)return l.output();if("base64"==
a.outputMode)return z(l.output());try{var n=l.output("blob");saveAs(n,a.fileName+".pdf")}catch(d){A(a.fileName+".pdf","data:application/pdf;base64,"+z(l.output()))}}function G(a,n,d){return a.replace(new RegExp(n.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1"),"g"),d)}function J(l,n,d){var e="";if(null!=l)if(l=v(l,n,d),n=null===l||""==l?"":l.toString(),l instanceof Date)e=a.csvEnclosure+l.toLocaleString()+a.csvEnclosure;else if(e=G(n,a.csvEnclosure,a.csvEnclosure+a.csvEnclosure),0<=e.indexOf(a.csvSeparator)||
/[\r\n ]/g.test(e))e=a.csvEnclosure+e+a.csvEnclosure;return e}function v(l,n,d){var e="";if(null!=l){l=g(l);if(!0===a.htmlContent)e=l.html().trim();else if(e=l.text().trim().replace(/\u00AD/g,""),a.numbers.html.decimalMark!=a.numbers.output.decimalMark||a.numbers.html.thousandsSeparator!=a.numbers.output.thousandsSeparator){var b;b=e||"0";b=G(b,a.numbers.html.decimalMark,".");b=G(b,a.numbers.html.thousandsSeparator,"");b="number"===typeof b||!1!==jQuery.isNumeric(b)?b:!1;if(!1!==b){e=(""+b).split(".");
1==e.length&&(e[1]="");var f=3<e[0].length?e[0].length%3:0,e=(0>b?"-":"")+(a.numbers.output.thousandsSeparator?(f?e[0].substr(0,f)+a.numbers.output.thousandsSeparator:"")+e[0].substr(f).replace(/(\d{3})(?=\d)/g,"$1"+a.numbers.output.thousandsSeparator):e[0])+(e[1].length?a.numbers.output.decimalMark+e[1]:"")}}!0===a.escape&&(e=escape(e));"function"===typeof a.onCellData&&(e=a.onCellData(l,n,d,e))}return e}function Q(a,b,d){return b+"-"+d.toLowerCase()}function B(a,b){try{return window.getComputedStyle?
(b=b.replace(/([a-z])([A-Z])/,Q),window.getComputedStyle(a,null).getPropertyValue(b)):a.currentStyle?a.currentStyle[b]:a.style[b]}catch(d){}return""}function H(a,b,d){b=B(a,b).match(/\d+/);if(null!==b){b=b[0];var e=document.createElement("div");e.style.overflow="hidden";e.style.visibility="hidden";a.parentElement.appendChild(e);e.style.width=100+d;d=100/e.offsetWidth;a.parentElement.removeChild(e);return b*d}return 0}function A(a,b){var d=document.createElement("a");if(d){document.body.appendChild(d);
d.style="display: none";d.download=a;d.href=b;if(document.createEvent)null==E&&(E=document.createEvent("MouseEvents")),E.initEvent("click",!0,!1),d.dispatchEvent(E);else if(document.createEventObject)d.fireEvent("onclick");else if("function"==typeof d.onclick)d.onclick();document.body.removeChild(d)}}function z(a){var b="",d,e,g,f,h,k,m=0;a=a.replace(/\x0d\x0a/g,"\n");e="";for(g=0;g<a.length;g++)f=a.charCodeAt(g),128>f?e+=String.fromCharCode(f):(127<f&&2048>f?e+=String.fromCharCode(f>>6|192):(e+=
String.fromCharCode(f>>12|224),e+=String.fromCharCode(f>>6&63|128)),e+=String.fromCharCode(f&63|128));for(a=e;m<a.length;)d=a.charCodeAt(m++),e=a.charCodeAt(m++),g=a.charCodeAt(m++),f=d>>2,d=(d&3)<<4|e>>4,h=(e&15)<<2|g>>6,k=g&63,isNaN(e)?h=k=64:isNaN(g)&&(k=64),b=b+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(f)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(d)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(h)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(k);
return b}var a={consoleLog:!1,csvEnclosure:'"',csvSeparator:",",csvUseBOM:!0,displayTableName:!1,escape:!1,excelstyles:["border-bottom","border-top","border-left","border-right"],fileName:"tableExport",htmlContent:!1,ignoreColumn:[],ignoreRow:[],jspdf:{orientation:"p",unit:"pt",format:"a4",margins:{left:20,right:10,top:10,bottom:10},autotable:{padding:2,lineHeight:12,fontSize:8,tableExport:{onAfterAutotable:null,onBeforeAutotable:null,onTable:null}}},numbers:{html:{decimalMark:".",thousandsSeparator:","},
output:{decimalMark:".",thousandsSeparator:","}},onCellData:null,outputMode:"file",tbodySelector:"tr",theadSelector:"tr",tableName:"myTableName",type:"csv",worksheetName:"xlsWorksheetName"},E=null,m=0,y=[],f="";g.extend(!0,a,t);if("csv"==a.type||"txt"==a.type){var p="",m=0;g(this).find("thead").first().find(a.theadSelector).each(function(){f="";u(this,"th,td",m,function(b,g,d){f+=J(b,g,d)+a.csvSeparator});f=g.trim(f).substring(0,f.length-1);0<f.length&&(0<p.length&&(p+="\n"),p+=f);m++});g(this).find("tbody").first().find(a.tbodySelector).each(function(){f=
"";u(this,"td",m,function(b,g,d){f+=J(b,g,d)+a.csvSeparator});f=g.trim(f).substring(0,f.length-1);0<f.length&&(0<p.length&&(p+="\n"),p+=f);m++});p+="\n";!0===a.consoleLog&&console.log(p);if("string"===a.outputMode)return p;if("base64"===a.outputMode)return z(p);try{var w=new Blob([("csv"==a.type&&a.csvUseBOM?"\ufeff":"")+p],{type:"text/"+("csv"==a.type?"csv":"plain")+";charset=utf-8"});saveAs(w,a.fileName+"."+a.type)}catch(R){A(a.fileName+"."+a.type,"data:text/"+("csv"==a.type?"csv":"plain")+";charset=utf-8,"+
("csv"==a.type&&a.csvUseBOM?"\ufeff":"")+encodeURIComponent(p))}}else if("sql"==a.type){var m=0,h="INSERT INTO `"+a.tableName+"` (";g(this).find("thead").first().find(a.theadSelector).each(function(){u(this,"th,td",m,function(a,b,d){h+="'"+v(a,b,d)+"',"});m++;h=g.trim(h);h=g.trim(h).substring(0,h.length-1)});h+=") VALUES ";g(this).find("tbody").first().find(a.tbodySelector).each(function(){f="";u(this,"td",m,function(a,b,d){f+="'"+v(a,b,d)+"',"});3<f.length&&(h+="("+f,h=g.trim(h).substring(0,h.length-
1),h+="),");m++});h=g.trim(h).substring(0,h.length-1);h+=";";!0===a.consoleLog&&console.log(h);if("string"==a.outputMode)return h;if("base64"==a.outputMode)return z(h);try{w=new Blob([h],{type:"text/plain;charset=utf-8"}),saveAs(w,a.fileName+".sql")}catch(S){A(a.fileName+".sql","data:application/sql;charset=utf-8,"+encodeURIComponent(h))}}else if("json"==a.type){var K=[];g(this).find("thead").first().find(a.theadSelector).each(function(){var a=[];u(this,"th,td",m,function(b,d,e){a.push(v(b,d,e))});
K.push(a)});var L=[];g(this).find("tbody").first().find(a.tbodySelector).each(function(){var a=[];u(this,"td",m,function(b,d,e){a.push(v(b,d,e))});0<a.length&&(1!=a.length||""!=a[0])&&L.push(a);m++});t=[];t.push({header:K,data:L});var b=JSON.stringify(t);!0===a.consoleLog&&console.log(b);if("string"==a.outputMode)return b;t=z(b);if("base64"==a.outputMode)return t;try{w=new Blob([b],{type:"application/json;charset=utf-8"}),saveAs(w,a.fileName+".json")}catch(T){A(a.fileName+".json","data:application/json;charset=utf-8;base64,"+
t)}}else if("xml"===a.type){var m=0,q='<?xml version="1.0" encoding="utf-8"?>',q=q+"<tabledata><fields>";g(this).find("thead").first().find(a.theadSelector).each(function(){u(this,"th,td",m,function(a,b,d){q+="<field>"+v(a,b,d)+"</field>"});m++});var q=q+"</fields><data>",M=1;g(this).find("tbody").first().find(a.tbodySelector).each(function(){var a=1;f="";u(this,"td",m,function(b,d,e){f+="<column-"+a+">"+v(b,d,e)+"</column-"+a+">";a++});0<f.length&&"<column-1></column-1>"!=f&&(q+='<row id="'+M+'">'+
f+"</row>",M++);m++});q+="</data></tabledata>";!0===a.consoleLog&&console.log(q);if("string"==a.outputMode)return q;t=z(q);if("base64"==a.outputMode)return t;try{w=new Blob([q],{type:"application/xml;charset=utf-8"}),saveAs(w,a.fileName+".xml")}catch(U){A(a.fileName+".xml","data:application/xml;charset=utf-8;base64,"+t)}}else if("excel"==a.type||"doc"==a.type){var m=0,x="<table>";g(this).find("thead").first().find(a.theadSelector).each(function(){f="";u(this,"th,td",m,function(b,n,d){if(null!=b){f+=
"<td style='";for(var e in a.excelstyles)a.excelstyles.hasOwnProperty(e)&&(f+=a.excelstyles[e]+": "+g(b).css(a.excelstyles[e])+";");f+="'>"+v(b,n,d)+"</td>"}});0<f.length&&(x+="<tr>"+f+"</tr>");m++});g(this).find("tbody").first().find(a.tbodySelector).each(function(){f="";u(this,"td",m,function(b,n,d){if(null!=b){f+="<td style='";for(var e in a.excelstyles)a.excelstyles.hasOwnProperty(e)&&(f+=a.excelstyles[e]+": "+g(b).css(a.excelstyles[e])+";");g(b).is("[colspan]")&&(f+="' colspan='"+g(b).attr("colspan"));
g(b).is("[rowspan]")&&(f+="' rowspan='"+g(b).attr("rowspan"));f+="'>"+v(b,n,d)+"</td>"}});0<f.length&&(x+="<tr>"+f+"</tr>");m++});a.displayTableName&&(x+="<tr><td></td></tr><tr><td></td></tr><tr><td>"+v(g("<p>"+a.tableName+"</p>"))+"</td></tr>");x+="</table>";!0===a.consoleLog&&console.log(x);b="<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+a.type+"' xmlns='http://www.w3.org/TR/REC-html40'>";b+='<meta http-equiv="content-type" content="application/vnd.ms-'+
a.type+'; charset=UTF-8">';b=b+'<meta http-equiv="content-type" content="application/'+("excel"===a.type?"vnd.ms-excel":"msword");b+='; charset=UTF-8">';b+="<head>";"excel"===a.type&&(b+="\x3c!--[if gte mso 9]>",b+="<xml>",b+="<x:ExcelWorkbook>",b+="<x:ExcelWorksheets>",b+="<x:ExcelWorksheet>",b+="<x:Name>",b+=a.worksheetName,b+="</x:Name>",b+="<x:WorksheetOptions>",b+="<x:DisplayGridlines/>",b+="</x:WorksheetOptions>",b+="</x:ExcelWorksheet>",b+="</x:ExcelWorksheets>",b+="</x:ExcelWorkbook>",b+=
"</xml>",b+="<![endif]--\x3e");b+="</head>";b+="<body>";b+=x;b+="</body>";b+="</html>";if("string"==a.outputMode)return b;t=z(b);if("base64"===a.outputMode)return t;var N="excel"===a.type?"xls":"doc";try{w=new Blob([b],{type:"application/vnd.ms-"+a.type}),saveAs(w,a.fileName+"."+N)}catch(V){A(a.fileName+"."+N,"data:application/vnd.ms-"+a.type+";base64,"+t)}}else if("png"==a.type)html2canvas(g(this),{onrendered:function(b){b=b.toDataURL();b=b.substring(22);for(var g=atob(b),d=new ArrayBuffer(g.length),
e=new Uint8Array(d),f=0;f<g.length;f++)e[f]=g.charCodeAt(f);try{var h=new Blob([d],{type:"image/png"});saveAs(h,a.fileName+".png")}catch(k){A(a.fileName+".png","data:image/png;base64,"+b)}}});else if("pdf"==a.type)if(!1===a.jspdf.autotable){var w={dim:{w:H(g(this).first().get(0),"width","mm"),h:H(g(this).first().get(0),"height","mm")},pagesplit:!1},O=new jsPDF(a.jspdf.orientation,a.jspdf.unit,a.jspdf.format);O.addHTML(g(this).first(),a.jspdf.margins.left,a.jspdf.margins.top,w,function(){I(O)})}else{var k=
a.jspdf.autotable.tableExport;if("string"===typeof a.jspdf.format&&"bestfit"===a.jspdf.format.toLowerCase()){var C={a0:[2383.94,3370.39],a1:[1683.78,2383.94],a2:[1190.55,1683.78],a3:[841.89,1190.55],a4:[595.28,841.89]},F="",D="",P=0;g(this).filter(":visible").each(function(){if("none"!=g(this).css("display")){var a=H(g(this).get(0),"width","pt");if(a>P){a>C.a0[0]&&(F="a0",D="l");for(var b in C)C.hasOwnProperty(b)&&C[b][1]>a&&(F=b,D="l",C[b][0]>a&&(D="p"));P=a}}});a.jspdf.format=""==F?"a4":F;a.jspdf.orientation=
""==D?"w":D}k.doc=new jsPDF(a.jspdf.orientation,a.jspdf.unit,a.jspdf.format);g(this).filter(":visible").each(function(){if("none"!=g(this).css("display")){var b,f=0,d={};k.columns=[];k.rows=[];k.rowoptions={};if("function"===typeof k.onTable&&!1===k.onTable(g(this),a))return!0;Object.keys(a.jspdf.autotable).forEach(function(b){d[b]=a.jspdf.autotable[b]});d.margins={};g.extend(!0,d.margins,a.jspdf.margins);"function"!==typeof d.renderHeaderCell&&(d.renderHeaderCell=function(a,b,d,e,f,g,h){var l=h.tableExport.doc,
k=0;l.setFillColor(52,73,94);l.setTextColor(255,255,255);l.setFontStyle("bold");l.rect(a,b,d,e,"F");b+=h.lineHeight/2+l.autoTableTextHeight()/2;"undefined"!=typeof h.tableExport.columns[f]&&(e=h.tableExport.columns[f],"undefined"!=typeof e.style&&("right"==e.style.align?k=d-l.getStringUnitWidth(""+g)*l.internal.getFontSize()-h.padding:"center"==e.style.align&&(k=(d-l.getStringUnitWidth(""+g)*l.internal.getFontSize())/2)));k<h.padding&&(k=h.padding);l.text(g,a+k,b)});"function"!==typeof d.renderCell&&
(d.renderCell=function(a,b,d,e,f,g,l,h){var k=h.tableExport.doc,m=0;k.setFillColor(0===l%2?245:255);k.setTextColor(50);k.rect(a,b,d,e,"F");b+=h.lineHeight/2+k.autoTableTextHeight()/2-2.5;e=h.tableExport.rowoptions[l+":"+f];"undefined"!=typeof e&&("right"==e.style.align?m=d-k.getStringUnitWidth(""+g)*k.internal.getFontSize()-h.padding:"center"==e.style.align&&(m=(d-k.getStringUnitWidth(""+g)*k.internal.getFontSize())/2));m<h.padding&&(m=h.padding);k.text(g,a+m,b)});g(this).find("thead").find(a.theadSelector).each(function(){b=
0;u(this,"th,td",f,function(a,e,d){var f=B(a,"text-align");"start"==f&&(f="rtl"==B(a,"direction")?"right":"left");a={title:v(a,e,d),key:b++,style:{align:f,bcolor:B(a,"background-color")}};k.columns.push(a)});f++});var e=0;g(this).find("tbody").find(a.tbodySelector).each(function(){var a=[];b=0;u(this,"td",f,function(d,f,g){var h=B(d,"text-align");"start"==h&&(h="rtl"==B(d,"direction")?"right":"left");h={style:{align:h,bcolor:B(d,"background-color")}};k.rowoptions[e+":"+b++]=h;a.push(v(d,f,g))});a.length&&
(k.rows.push(a),e++);f++});if("function"===typeof k.onBeforeAutotable)k.onBeforeAutotable(g(this),k.columns,k.rows,d);k.doc.autoTable(k.columns,k.rows,d);if("function"===typeof k.onAfterAutotable)k.onAfterAutotable(g(this),d);a.jspdf.autotable.startY=k.doc.autoTableEndPosY()+d.margins.top}});I(k.doc);k.columns.length=0;k.rows.length=0;delete k.doc;k.doc=null}return this}})})(jQuery);