forked from hhurz/tableExport.jquery.plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableExport.min.js
More file actions
32 lines (32 loc) · 15.8 KB
/
tableExport.min.js
File metadata and controls
32 lines (32 loc) · 15.8 KB
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
28
29
30
31
32
(function(d){d.fn.extend({tableExport:function(q){function y(b,m,c,g,k){if(-1==d.inArray(c,a.ignoreRow)&&-1==d.inArray(c-g,a.ignoreRow)){var M=d(b).filter(function(){return"none"!=d(this).data("tableexport-display")&&(d(this).is(":visible")||"always"==d(this).data("tableexport-display")||"always"==d(this).closest("table").data("tableexport-display"))}).find(m),e=0;M.each(function(b){if(("always"==d(this).data("tableexport-display")||"none"!=d(this).css("display")&&"hidden"!=d(this).css("visibility")&&
"none"!=d(this).data("tableexport-display"))&&-1==d.inArray(b,a.ignoreColumn)&&-1==d.inArray(b-M.length,a.ignoreColumn)&&"function"===typeof k){var m,g=0,f,h=0;if("undefined"!=typeof B[c]&&0<B[c].length)for(m=0;m<=b;m++)"undefined"!=typeof B[c][m]&&(k(null,c,m),delete B[c][m],b++);d(this).is("[colspan]")&&(g=parseInt(d(this).attr("colspan")),e+=0<g?g-1:0);d(this).is("[rowspan]")&&(h=parseInt(d(this).attr("rowspan")));k(this,c,b);for(m=0;m<g-1;m++)k(null,c,b+m);if(h)for(f=1;f<h;f++)for("undefined"==
typeof B[c+f]&&(B[c+f]=[]),B[c+f][b+e]="",m=1;m<g;m++)B[c+f][b+e-m]=""}})}}function N(b){!0===a.consoleLog&&console.log(b.output());if("string"==a.outputMode)return b.output();if("base64"==a.outputMode)return C(b.output());try{var m=b.output("blob");saveAs(m,a.fileName+".pdf")}catch(c){D(a.fileName+".pdf","data:application/pdf;base64,"+C(b.output()))}}function O(b,a,c){var g=0;"undefined"!=typeof c&&(g=c.colspan);if(0<=g){for(var k=b.width,d=b.textPos.x,e=a.table.columns.indexOf(a.column),f=1;f<g;f++)k+=
a.table.columns[e+f].width;1<g&&("right"===b.styles.halign?d=b.textPos.x+k-b.width:"center"===b.styles.halign&&(d=b.textPos.x+(k-b.width)/2));b.width=k;b.textPos.x=d;1<c.rowspan&&("middle"===b.styles.valign?b.textPos.y+=b.height*(c.rowspan-1)/2:"bottom"===b.styles.valign&&(b.textPos.y+=(c.rowspan-1)*b.height),b.height*=c.rowspan);if("middle"===b.styles.valign||"bottom"===b.styles.valign)c=("string"===typeof b.text?b.text.split(/\r\n|\r|\n/g):b.text).length||1,2<c&&(b.textPos.y-=(2-1.15)/2*a.row.styles.fontSize*
(c-2)/3);return!0}return!1}function J(b,a,c){return b.replace(new RegExp(a.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1"),"g"),c)}function W(b){b=J(b||"0",a.numbers.html.decimalMark,".");b=J(b,a.numbers.html.thousandsSeparator,"");return"number"===typeof b||!1!==jQuery.isNumeric(b)?b:!1}function w(b,m,c){var g="";if(null!=b){b=d(b);var k=b.html();"function"===typeof a.onCellHtmlData&&(k=a.onCellHtmlData(b,m,c,k));if(!0===a.htmlContent)g=d.trim(k);else{var e=k.replace(/\n/g,"\u2028").replace(/<br\s*[\/]?>/gi,
"\u2060"),k=d("<div/>").html(e).contents(),e="";d.each(k.text().split("\u2028"),function(b,a){0<b&&(e+=" ");e+=d.trim(a)});d.each(e.split("\u2060"),function(b,a){0<b&&(g+="\n");g+=d.trim(a).replace(/\u00AD/g,"")});if(a.numbers.html.decimalMark!=a.numbers.output.decimalMark||a.numbers.html.thousandsSeparator!=a.numbers.output.thousandsSeparator)if(k=W(g),!1!==k){var f=(""+k).split(".");1==f.length&&(f[1]="");var h=3<f[0].length?f[0].length%3:0,g=(0>k?"-":"")+(a.numbers.output.thousandsSeparator?(h?
f[0].substr(0,h)+a.numbers.output.thousandsSeparator:"")+f[0].substr(h).replace(/(\d{3})(?=\d)/g,"$1"+a.numbers.output.thousandsSeparator):f[0])+(f[1].length?a.numbers.output.decimalMark+f[1]:"")}}!0===a.escape&&(g=escape(g));"function"===typeof a.onCellData&&(g=a.onCellData(b,m,c,g))}return g}function X(b,a,c){return a+"-"+c.toLowerCase()}function P(b,a){var c=/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/.exec(b),g=a;c&&(g=[parseInt(c[1]),parseInt(c[2]),parseInt(c[3])]);return g}function Q(b){var a=
E(b,"text-align"),c=E(b,"font-weight"),g=E(b,"font-style"),k="";"start"==a&&(a="rtl"==E(b,"direction")?"right":"left");700<=c&&(k="bold");"italic"==g&&(k+=g);""==k&&(k="normal");return{style:{align:a,bcolor:P(E(b,"background-color"),[255,255,255]),color:P(E(b,"color"),[0,0,0]),fstyle:k},colspan:parseInt(d(b).attr("colspan"))||0,rowspan:parseInt(d(b).attr("rowspan"))||0}}function E(b,a){try{return window.getComputedStyle?(a=a.replace(/([a-z])([A-Z])/,X),window.getComputedStyle(b,null).getPropertyValue(a)):
b.currentStyle?b.currentStyle[a]:b.style[a]}catch(c){}return""}function K(b,a,c){a=E(b,a).match(/\d+/);if(null!==a){a=a[0];var g=document.createElement("div");g.style.overflow="hidden";g.style.visibility="hidden";b.parentElement.appendChild(g);g.style.width=100+c;c=100/g.offsetWidth;b.parentElement.removeChild(g);return a*c}return 0}function D(a,d){var c=document.createElement("a");if(c){c.style.display="none";c.download=a;c.href=d;document.body.appendChild(c);if(document.createEvent)null==H&&(H=
document.createEvent("MouseEvents")),H.initEvent("click",!0,!1),c.dispatchEvent(H);else if(document.createEventObject)c.fireEvent("onclick");else if("function"==typeof c.onclick)c.onclick();document.body.removeChild(c)}}function C(a){var d="",c,g,k,f,e,h,l=0;a=a.replace(/\x0d\x0a/g,"\n");g="";for(k=0;k<a.length;k++)f=a.charCodeAt(k),128>f?g+=String.fromCharCode(f):(127<f&&2048>f?g+=String.fromCharCode(f>>6|192):(g+=String.fromCharCode(f>>12|224),g+=String.fromCharCode(f>>6&63|128)),g+=String.fromCharCode(f&
63|128));for(a=g;l<a.length;)c=a.charCodeAt(l++),g=a.charCodeAt(l++),k=a.charCodeAt(l++),f=c>>2,c=(c&3)<<4|g>>4,e=(g&15)<<2|k>>6,h=k&63,isNaN(g)?e=h=64:isNaN(k)&&(h=64),d=d+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(f)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(c)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(e)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(h);return d}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:{styles:{cellPadding:2,rowHeight:12,fontSize:8,fillColor:255,textColor:50,fontStyle:"normal",overflow:"ellipsize",halign:"left",valign:"middle"},headerStyles:{fillColor:[52,73,94],textColor:255,
fontStyle:"bold",halign:"center"},alternateRowStyles:{fillColor:245},tableExport:{onAfterAutotable:null,onBeforeAutotable:null,onTable:null}}},numbers:{html:{decimalMark:".",thousandsSeparator:","},output:{decimalMark:".",thousandsSeparator:","}},onCellData:null,onCellHtmlData:null,outputMode:"file",tbodySelector:"tr",theadSelector:"tr",tableName:"myTableName",type:"csv",worksheetName:"xlsWorksheetName"},u=this,H=null,r=[],p=[],n=0,B=[],f="";d.extend(!0,a,q);if("csv"==a.type||"txt"==a.type){q=function(b,
e,c,g){p=d(u).find(b).first().find(e);p.each(function(){f="";y(this,c,n,g+p.length,function(b,c,d){var g=f,e="";if(null!=b)if(b=w(b,c,d),c=null===b||""==b?"":b.toString(),b instanceof Date)e=a.csvEnclosure+b.toLocaleString()+a.csvEnclosure;else if(e=J(c,a.csvEnclosure,a.csvEnclosure+a.csvEnclosure),0<=e.indexOf(a.csvSeparator)||/[\r\n ]/g.test(e))e=a.csvEnclosure+e+a.csvEnclosure;f=g+(e+a.csvSeparator)});f=d.trim(f).substring(0,f.length-1);0<f.length&&(0<x.length&&(x+="\n"),x+=f);n++});return p.length};
var x="",t=0,n=0,t=t+q("thead",a.theadSelector,"th,td",t),t=t+q("tbody",a.tbodySelector,"td",t);q("tfoot",a.tbodySelector,"td",t);x+="\n";!0===a.consoleLog&&console.log(x);if("string"===a.outputMode)return x;if("base64"===a.outputMode)return C(x);try{var z=new Blob([x],{type:"text/"+("csv"==a.type?"csv":"plain")+";charset=utf-8"});saveAs(z,a.fileName+"."+a.type,"csv"!=a.type||!1===a.csvUseBOM)}catch(b){D(a.fileName+"."+a.type,"data:text/"+("csv"==a.type?"csv":"plain")+";charset=utf-8,"+encodeURIComponent(x))}}else if("sql"==
a.type){var n=0,l="INSERT INTO `"+a.tableName+"` (",r=d(u).find("thead").first().find(a.theadSelector);r.each(function(){y(this,"th,td",n,r.length,function(a,d,c){l+="'"+w(a,d,c)+"',"});n++;l=d.trim(l);l=d.trim(l).substring(0,l.length-1)});l+=") VALUES ";p=d(u).find("tbody").first().find(a.tbodySelector);p.each(function(){f="";y(this,"td",n,r.length+p.length,function(a,d,c){f+="'"+w(a,d,c)+"',"});3<f.length&&(l+="("+f,l=d.trim(l).substring(0,l.length-1),l+="),");n++});l=d.trim(l).substring(0,l.length-
1);l+=";";!0===a.consoleLog&&console.log(l);if("string"==a.outputMode)return l;if("base64"==a.outputMode)return C(l);try{z=new Blob([l],{type:"text/plain;charset=utf-8"}),saveAs(z,a.fileName+".sql")}catch(b){D(a.fileName+".sql","data:application/sql;charset=utf-8,"+encodeURIComponent(l))}}else if("json"==a.type){var R=[],r=d(u).find("thead").first().find(a.theadSelector);r.each(function(){var a=[];y(this,"th,td",n,r.length,function(d,c,g){a.push(w(d,c,g))});R.push(a)});var S=[],p=d(u).find("tbody").first().find(a.tbodySelector);
p.each(function(){var a=[];y(this,"td",n,r.length+p.length,function(d,c,g){a.push(w(d,c,g))});0<a.length&&(1!=a.length||""!=a[0])&&S.push(a);n++});q=[];q.push({header:R,data:S});t=JSON.stringify(q);!0===a.consoleLog&&console.log(t);if("string"==a.outputMode)return t;q=C(t);if("base64"==a.outputMode)return q;try{z=new Blob([t],{type:"application/json;charset=utf-8"}),saveAs(z,a.fileName+".json")}catch(b){D(a.fileName+".json","data:application/json;charset=utf-8;base64,"+q)}}else if("xml"===a.type){var n=
0,v='<?xml version="1.0" encoding="utf-8"?>',v=v+"<tabledata><fields>",r=d(u).find("thead").first().find(a.theadSelector);r.each(function(){y(this,"th,td",n,p.length,function(a,d,c){v+="<field>"+w(a,d,c)+"</field>"});n++});var v=v+"</fields><data>",T=1,p=d(u).find("tbody").first().find(a.tbodySelector);p.each(function(){var a=1;f="";y(this,"td",n,r.length+p.length,function(d,c,g){f+="<column-"+a+">"+w(d,c,g)+"</column-"+a+">";a++});0<f.length&&"<column-1></column-1>"!=f&&(v+='<row id="'+T+'">'+f+
"</row>",T++);n++});v+="</data></tabledata>";!0===a.consoleLog&&console.log(v);if("string"==a.outputMode)return v;q=C(v);if("base64"==a.outputMode)return q;try{z=new Blob([v],{type:"application/xml;charset=utf-8"}),saveAs(z,a.fileName+".xml")}catch(b){D(a.fileName+".xml","data:application/xml;charset=utf-8;base64,"+q)}}else if("excel"==a.type||"xls"==a.type||"word"==a.type||"doc"==a.type){var t="excel"==a.type||"xls"==a.type?"excel":"word",L="excel"==t?"xls":"doc";q="xls"==L?'xmlns:x="urn:schemas-microsoft-com:office:excel"':
'xmlns:w="urn:schemas-microsoft-com:office:word"';var n=0,A="<table>",r=d(u).find("thead").first().find(a.theadSelector);r.each(function(){f="";y(this,"th,td",n,r.length,function(b,e,c){if(null!=b){f+='<td style="';for(var g in a.excelstyles)a.excelstyles.hasOwnProperty(g)&&(f+=a.excelstyles[g]+": "+d(b).css(a.excelstyles[g])+";");f+='">'+w(b,e,c)+"</td>"}});0<f.length&&(A+="<tr>"+f+"</tr>");n++});p=d(u).find("tbody").first().find(a.tbodySelector);p.each(function(){f="";y(this,"td",n,r.length+p.length,
function(b,e,c){if(null!=b){f+='<td style="';for(var g in a.excelstyles)a.excelstyles.hasOwnProperty(g)&&(f+=a.excelstyles[g]+": "+d(b).css(a.excelstyles[g])+";");d(b).is("[colspan]")&&(f+='" colspan="'+d(b).attr("colspan"));d(b).is("[rowspan]")&&(f+='" rowspan="'+d(b).attr("rowspan"));f+='">'+w(b,e,c)+"</td>"}});0<f.length&&(A+="<tr>"+f+"</tr>");n++});a.displayTableName&&(A+="<tr><td></td></tr><tr><td></td></tr><tr><td>"+w(d("<p>"+a.tableName+"</p>"))+"</td></tr>");A+="</table>";!0===a.consoleLog&&
console.log(A);var e;e='<html xmlns:o="urn:schemas-microsoft-com:office:office" '+q+' xmlns="http://www.w3.org/TR/REC-html40">'+('<meta http-equiv="content-type" content="application/vnd.ms-'+t+'; charset=UTF-8">');e+="<head>";"excel"===t&&(e+="\x3c!--[if gte mso 9]>",e+="<xml>",e+="<x:ExcelWorkbook>",e+="<x:ExcelWorksheets>",e+="<x:ExcelWorksheet>",e+="<x:Name>",e+=a.worksheetName,e+="</x:Name>",e+="<x:WorksheetOptions>",e+="<x:DisplayGridlines/>",e+="</x:WorksheetOptions>",e+="</x:ExcelWorksheet>",
e+="</x:ExcelWorksheets>",e+="</x:ExcelWorkbook>",e+="</xml>",e+="<![endif]--\x3e");e+="</head>";e+="<body>";e+=A;e+="</body>";e+="</html>";if("string"==a.outputMode)return e;q=C(e);if("base64"===a.outputMode)return q;try{z=new Blob([e],{type:"application/vnd.ms-"+a.type}),saveAs(z,a.fileName+"."+L)}catch(b){D(a.fileName+"."+L,"data:application/vnd.ms-"+t+";base64,"+q)}}else if("png"==a.type)html2canvas(d(u)[0],{allowTaint:!0,background:"#fff",onrendered:function(b){b=b.toDataURL();b=b.substring(22);
for(var d=atob(b),c=new ArrayBuffer(d.length),e=new Uint8Array(c),f=0;f<d.length;f++)e[f]=d.charCodeAt(f);try{var h=new Blob([c],{type:"image/png"});saveAs(h,a.fileName+".png")}catch(l){D(a.fileName+".png","data:image/png;base64,"+b)}}});else if("pdf"==a.type)if(!1===a.jspdf.autotable){var z={dim:{w:K(d(u).first().get(0),"width","mm"),h:K(d(u).first().get(0),"height","mm")},pagesplit:!1},U=new jsPDF(a.jspdf.orientation,a.jspdf.unit,a.jspdf.format);U.addHTML(d(u).first(),a.jspdf.margins.left,a.jspdf.margins.top,
z,function(){N(U)})}else{var h=a.jspdf.autotable.tableExport;if("string"===typeof a.jspdf.format&&"bestfit"===a.jspdf.format.toLowerCase()){var F={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]},I="",G="",V=0;d(u).filter(":visible").each(function(){if("none"!=d(this).css("display")){var a=K(d(this).get(0),"width","pt");if(a>V){a>F.a0[0]&&(I="a0",G="l");for(var e in F)F.hasOwnProperty(e)&&F[e][1]>a&&(I=e,G="l",F[e][0]>a&&(G="p"));V=a}}});a.jspdf.format=
""==I?"a4":I;a.jspdf.orientation=""==G?"w":G}h.doc=new jsPDF(a.jspdf.orientation,a.jspdf.unit,a.jspdf.format);d(u).filter(function(){return"none"!=d(this).data("tableexport-display")&&(d(this).is(":visible")||"always"==d(this).data("tableexport-display"))}).each(function(){var b,e=0;h.columns=[];h.rows=[];h.rowoptions={};if("function"===typeof h.onTable&&!1===h.onTable(d(this),a))return!0;a.jspdf.autotable.tableExport=null;var c=d.extend(!0,{},a.jspdf.autotable);a.jspdf.autotable.tableExport=h;c.margin=
{};d.extend(!0,c.margin,a.jspdf.margins);c.tableExport=h;"function"!==typeof c.beforePageContent&&(c.beforePageContent=function(a){1==a.pageCount&&a.table.rows.concat(a.table.headerRow).forEach(function(b){0<b.height&&(b.height+=(2-1.15)/2*b.styles.fontSize,a.table.height+=(2-1.15)/2*b.styles.fontSize)})});"function"!==typeof c.createdHeaderCell&&(c.createdHeaderCell=function(a,b){if("undefined"!=typeof h.columns[b.column.dataKey]){var d=h.columns[b.column.dataKey];a.styles.halign=d.style.align;"inherit"===
c.styles.fillColor&&(a.styles.fillColor=d.style.bcolor);"inherit"===c.styles.textColor&&(a.styles.textColor=d.style.color);"inherit"===c.styles.fontStyle&&(a.styles.fontStyle=d.style.fstyle)}});"function"!==typeof c.createdCell&&(c.createdCell=function(a,b){var d=h.rowoptions[b.row.index+":"+b.column.dataKey];"undefined"!=typeof d&&"undefined"!=typeof d.style&&(a.styles.halign=d.style.align,"inherit"===c.styles.fillColor&&(a.styles.fillColor=d.style.bcolor),"inherit"===c.styles.textColor&&(a.styles.textColor=
d.style.color),"inherit"===c.styles.fontStyle&&(a.styles.fontStyle=d.style.fstyle))});"function"!==typeof c.drawHeaderCell&&(c.drawHeaderCell=function(a,b){var c=h.columns[b.column.dataKey];return 1!=c.style.hasOwnProperty("hidden")||!0!==c.style.hidden?O(a,b,c):!1});"function"!==typeof c.drawCell&&(c.drawCell=function(a,b){return O(a,b,h.rowoptions[b.row.index+":"+b.column.dataKey])});r=d(this).find("thead").find(a.theadSelector);r.each(function(){b=0;y(this,"th,td",e,r.length,function(a,c,d){var e=
Q(a);e.title=w(a,c,d);e.key=b++;h.columns.push(e)});e++});var f=0;p=d(this).find("tbody").find(a.tbodySelector);p.each(function(){var a=[];b=0;y(this,"td",e,r.length+p.length,function(c,e,l){if("undefined"===typeof h.columns[b]){var m={title:"",key:b,style:{hidden:!0}};h.columns.push(m)}null!==c?h.rowoptions[f+":"+b++]=Q(c):(m=d.extend(!0,{},h.rowoptions[f+":"+(b-1)]),m.colspan=-1,h.rowoptions[f+":"+b++]=m);a.push(w(c,e,l))});a.length&&(h.rows.push(a),f++);e++});if("function"===typeof h.onBeforeAutotable)h.onBeforeAutotable(d(this),
h.columns,h.rows,c);h.doc.autoTable(h.columns,h.rows,c);if("function"===typeof h.onAfterAutotable)h.onAfterAutotable(d(this),c);a.jspdf.autotable.startY=h.doc.autoTableEndPosY()+c.margin.top});N(h.doc);h.columns.length=0;h.rows.length=0;delete h.doc;h.doc=null}return this}})})(jQuery);