123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725 |
- /**
- * @preserve tableExport.jquery.plugin
- *
- * Copyright (c) 2015,2016 hhurz, https://github.com/hhurz/tableExport.jquery.plugin
- * Original work Copyright (c) 2014 Giri Raj, https://github.com/kayalshri/
- *
- * Licensed under the MIT License, http://opensource.org/licenses/mit-license
- */
- (function ($) {
- $.fn.extend({
- tableExport: function (options) {
- var defaults = {
- consoleLog: false,
- csvEnclosure: '"',
- csvSeparator: ',',
- csvUseBOM: true,
- displayTableName: false,
- escape: false,
- excelstyles: [], // e.g. ['border-bottom', 'border-top', 'border-left', 'border-right']
- fileName: 'tableExport',
- htmlContent: false,
- ignoreColumn: [],
- ignoreRow:[],
- jsonScope: 'all', // head, data, all
- jspdf: {orientation: 'p',
- unit: 'pt',
- format: 'a4', // jspdf page format or 'bestfit' for autmatic paper format selection
- margins: {left: 20, right: 10, top: 10, bottom: 10},
- autotable: {styles: {cellPadding: 2,
- rowHeight: 12,
- fontSize: 8,
- fillColor: 255, // color value or 'inherit' to use css background-color from html table
- textColor: 50, // color value or 'inherit' to use css color from html table
- fontStyle: 'normal', // normal, bold, italic, bolditalic or 'inherit' to use css font-weight and fonst-style from html table
- overflow: 'ellipsize', // visible, hidden, ellipsize or linebreak
- halign: 'left', // left, center, right
- valign: 'middle' // top, middle, bottom
- },
- headerStyles: {fillColor: [52, 73, 94],
- textColor: 255,
- fontStyle: 'bold',
- halign: 'center'
- },
- alternateRowStyles: {fillColor: 245
- },
- tableExport: {onAfterAutotable: null,
- onBeforeAutotable: null,
- onTable: null,
- outputImages: true
- }
- }
- },
- numbers: {html: {decimalMark: '.',
- thousandsSeparator: ','
- },
- output: {decimalMark: '.',
- thousandsSeparator: ','
- }
- },
- onCellData: null,
- onCellHtmlData: null,
- outputMode: 'file', // 'file', 'string', 'base64' or 'window' (experimental)
- pdfmake: {enabled: false}, // true: use pdfmake instead of jspdf(-autotable)
- tbodySelector: 'tr',
- tfootSelector: 'tr', // set empty ('') to prevent export of tfoot rows
- theadSelector: 'tr',
- tableName: 'myTableName',
- type: 'csv', // 'csv', 'txt', 'sql', 'json', 'xml', 'excel', 'doc', 'png' or 'pdf'
- worksheetName: 'xlsWorksheetName'
- };
- var FONT_ROW_RATIO = 1.15;
- var el = this;
- var DownloadEvt = null;
- var $hrows = [];
- var $rows = [];
- var rowIndex = 0;
- var rowspans = [];
- var trData = '';
- var colNames = [];
- var blob;
- $.extend(true, defaults, options);
- colNames = GetColumnNames (el);
- if (defaults.type == 'csv' || defaults.type == 'txt') {
- var csvData = "";
- var rowlength = 0;
- rowIndex = 0;
- var CollectCsvData = function ($rows, rowselector, length) {
- $rows.each(function () {
- trData = "";
- ForEachVisibleCell(this, rowselector, rowIndex, length + $rows.length,
- function (cell, row, col) {
- trData += csvString(cell, row, col) + defaults.csvSeparator;
- });
- trData = $.trim(trData).substring(0, trData.length - 1);
- if (trData.length > 0) {
- if (csvData.length > 0)
- csvData += "\n";
- csvData += trData;
- }
- rowIndex++;
- });
- return $rows.length;
- };
- rowlength += CollectCsvData ($(el).find('thead').first().find(defaults.theadSelector), 'th,td', rowlength);
- $(el).find('tbody').each(function() {
- rowlength += CollectCsvData ($(this).find(defaults.tbodySelector), 'td,th', rowlength);
- });
- if (defaults.tfootSelector.length)
- CollectCsvData ($(el).find('tfoot').first().find(defaults.tfootSelector), 'td,th', rowlength);
- csvData += "\n";
- //output
- if (defaults.consoleLog === true)
- console.log(csvData);
- if (defaults.outputMode === 'string')
- return csvData;
- if (defaults.outputMode === 'base64')
- return base64encode(csvData);
- if (defaults.outputMode === 'window') {
- downloadFile(false, 'data:text/' + (defaults.type == 'csv' ? 'csv' : 'plain') + ';charset=utf-8,', csvData);
- return;
- }
- try {
- blob = new Blob([csvData], {type: "text/" + (defaults.type == 'csv' ? 'csv' : 'plain') + ";charset=utf-8"});
- saveAs(blob, defaults.fileName + '.' + defaults.type, (defaults.type != 'csv' || defaults.csvUseBOM === false));
- }
- catch (e) {
- downloadFile(defaults.fileName + '.' + defaults.type,
- 'data:text/' + (defaults.type == 'csv' ? 'csv' : 'plain') + ';charset=utf-8,' + ((defaults.type == 'csv' && defaults.csvUseBOM)? '\ufeff' : ''),
- csvData);
- }
- } else if (defaults.type == 'sql') {
- // Header
- rowIndex = 0;
- var tdData = "INSERT INTO `" + defaults.tableName + "` (";
- $hrows = $(el).find('thead').first().find(defaults.theadSelector);
- $hrows.each(function () {
- ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
- function (cell, row, col) {
- tdData += "'" + parseString(cell, row, col) + "',";
- });
- rowIndex++;
- tdData = $.trim(tdData);
- tdData = $.trim(tdData).substring(0, tdData.length - 1);
- });
- tdData += ") VALUES ";
- // Row vs Column
- $(el).find('tbody').each(function() {
- $rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
- });
- if (defaults.tfootSelector.length)
- $rows.push.apply ($rows, $(el).find('tfoot').find(defaults.tfootSelector));
- $($rows).each(function () {
- trData = "";
- ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
- function (cell, row, col) {
- trData += "'" + parseString(cell, row, col) + "',";
- });
- if (trData.length > 3) {
- tdData += "(" + trData;
- tdData = $.trim(tdData).substring(0, tdData.length - 1);
- tdData += "),";
- }
- rowIndex++;
- });
- tdData = $.trim(tdData).substring(0, tdData.length - 1);
- tdData += ";";
- //output
- if (defaults.consoleLog === true)
- console.log(tdData);
- if (defaults.outputMode === 'string')
- return tdData;
- if (defaults.outputMode === 'base64')
- return base64encode(tdData);
- try {
- blob = new Blob([tdData], {type: "text/plain;charset=utf-8"});
- saveAs(blob, defaults.fileName + '.sql');
- }
- catch (e) {
- downloadFile(defaults.fileName + '.sql',
- 'data:application/sql;charset=utf-8,',
- tdData);
- }
- } else if (defaults.type == 'json') {
- var jsonHeaderArray = [];
- $hrows = $(el).find('thead').first().find(defaults.theadSelector);
- $hrows.each(function () {
- var jsonArrayTd = [];
- ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
- function (cell, row, col) {
- jsonArrayTd.push(parseString(cell, row, col));
- });
- jsonHeaderArray.push(jsonArrayTd);
- });
- var jsonArray = [];
- $(el).find('tbody').each(function() {
- $rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
- });
- if (defaults.tfootSelector.length)
- $rows.push.apply ($rows, $(el).find('tfoot').find(defaults.tfootSelector));
- $($rows).each(function () {
- var jsonObjectTd = {};
- var colIndex = 0;
- ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
- function (cell, row, col) {
- if (jsonHeaderArray.length) {
- jsonObjectTd[jsonHeaderArray[jsonHeaderArray.length-1][colIndex]] = parseString(cell, row, col);
- } else {
- jsonObjectTd[colIndex] = parseString(cell, row, col);
- }
- colIndex++;
- });
- if ($.isEmptyObject(jsonObjectTd) === false)
- jsonArray.push(jsonObjectTd);
- rowIndex++;
- });
- var sdata = "";
- if (defaults.jsonScope == 'head')
- sdata = JSON.stringify(jsonHeaderArray);
- else if (defaults.jsonScope == 'data')
- sdata = JSON.stringify(jsonArray);
- else // all
- sdata = JSON.stringify({header: jsonHeaderArray, data: jsonArray});
- if (defaults.consoleLog === true)
- console.log(sdata);
- if (defaults.outputMode === 'string')
- return sdata;
- if (defaults.outputMode === 'base64')
- return base64encode(sdata);
- try {
- blob = new Blob([sdata], {type: "application/json;charset=utf-8"});
- saveAs(blob, defaults.fileName + '.json');
- }
- catch (e) {
- downloadFile(defaults.fileName + '.json',
- 'data:application/json;charset=utf-8;base64,',
- sdata);
- }
- } else if (defaults.type === 'xml') {
- rowIndex = 0;
- var xml = '<?xml version="1.0" encoding="utf-8"?>';
- xml += '<tabledata><fields>';
- // Header
- $hrows = $(el).find('thead').first().find(defaults.theadSelector);
- $hrows.each(function () {
- ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
- function (cell, row, col) {
- xml += "<field>" + parseString(cell, row, col) + "</field>";
- });
- rowIndex++;
- });
- xml += '</fields><data>';
- // Row Vs Column
- var rowCount = 1;
- $(el).find('tbody').each(function() {
- $rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
- });
- if (defaults.tfootSelector.length)
- $rows.push.apply ($rows, $(el).find('tfoot').find(defaults.tfootSelector));
- $($rows).each(function () {
- var colCount = 1;
- trData = "";
- ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
- function (cell, row, col) {
- trData += "<column-" + colCount + ">" + parseString(cell, row, col) + "</column-" + colCount + ">";
- colCount++;
- });
- if (trData.length > 0 && trData != "<column-1></column-1>") {
- xml += '<row id="' + rowCount + '">' + trData + '</row>';
- rowCount++;
- }
- rowIndex++;
- });
- xml += '</data></tabledata>';
- //output
- if (defaults.consoleLog === true)
- console.log(xml);
- if (defaults.outputMode === 'string')
- return xml;
- if (defaults.outputMode === 'base64')
- return base64encode(xml);
- try {
- blob = new Blob([xml], {type: "application/xml;charset=utf-8"});
- saveAs(blob, defaults.fileName + '.xml');
- }
- catch (e) {
- downloadFile(defaults.fileName + '.xml',
- 'data:application/xml;charset=utf-8;base64,',
- xml);
- }
- } else if (defaults.type == 'excel' || defaults.type == 'xls' || defaults.type == 'word' || defaults.type == 'doc') {
- var MSDocType = (defaults.type == 'excel' || defaults.type == 'xls') ? 'excel' : 'word';
- var MSDocExt = (MSDocType == 'excel') ? 'xls' : 'doc';
- var MSDocSchema = 'xmlns:x="urn:schemas-microsoft-com:office:' + MSDocType + '"';
- var $tables = $(el).filter(function() {
- return $(this).data("tableexport-display") != 'none' &&
- ($(this).is(':visible') ||
- $(this).data("tableexport-display") == 'always');
- });
- var docData = '';
- $tables.each(function(){
- var $table = $(this);
- rowIndex = 0;
- colNames = GetColumnNames (this);
- docData += '<table><thead>';
- // Header
- $hrows = $table.find('thead').first().find(defaults.theadSelector);
- $hrows.each(function() {
- trData = "";
- ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
- function(cell, row, col) {
- if (cell !== null) {
- var thstyle = '';
- trData += '<th';
- for (var styles in defaults.excelstyles) {
- if (defaults.excelstyles.hasOwnProperty(styles)) {
- var thcss = $(cell).css(defaults.excelstyles[styles]);
- if (thcss !== '' && thcss !='0px none rgb(0, 0, 0)' && thcss != 'rgba(0, 0, 0, 0)') {
- thstyle += (thstyle === '') ? 'style="' : ';';
- thstyle += defaults.excelstyles[styles] + ':' + thcss;
- }
- }
- }
- if (thstyle !== '' )
- trData += ' ' + thstyle + '"';
- if ($(cell).is("[colspan]"))
- trData += ' colspan="' + $(cell).attr('colspan') + '"';
- if ($(cell).is("[rowspan]"))
- trData += ' rowspan="' + $(cell).attr('rowspan') + '"';
- trData += '>' + parseString(cell, row, col) + '</th>';
- }
- });
- if (trData.length > 0)
- docData += '<tr>' + trData + '</tr>';
- rowIndex++;
- });
- docData += '</thead><tbody>';
- // Row Vs Column, support multiple tbodys
- $table.find('tbody').each(function() {
- $rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
- });
- if (defaults.tfootSelector.length)
- $rows.push.apply ($rows, $table.find('tfoot').find(defaults.tfootSelector));
- $($rows).each(function() {
- var $row = $(this);
- trData = "";
- ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
- function(cell, row, col) {
- if (cell !== null) {
- var tdstyle = '';
- var tdcss = $(cell).data("tableexport-msonumberformat");
- if (typeof tdcss == 'undefined' && typeof defaults.onMsoNumberFormat === 'function')
- tdcss = defaults.onMsoNumberFormat(cell, row, col);
- if (typeof tdcss != 'undefined' && tdcss !== '')
- tdstyle = 'style="mso-number-format:\'' + tdcss + '\'';
- for (var cssStyle in defaults.excelstyles) {
- if (defaults.excelstyles.hasOwnProperty(cssStyle)) {
- tdcss = $(cell).css(defaults.excelstyles[cssStyle]);
- if (tdcss === '')
- tdcss = $row.css(defaults.excelstyles[cssStyle]);
- if (tdcss !== '' && tdcss !='0px none rgb(0, 0, 0)' && tdcss != 'rgba(0, 0, 0, 0)') {
- tdstyle += (tdstyle === '') ? 'style="' : ';';
- tdstyle += defaults.excelstyles[cssStyle] + ':' + tdcss;
- }
- }
- }
- trData += '<td';
- if (tdstyle !== '' )
- trData += ' ' + tdstyle + '"';
- if ($(cell).is("[colspan]"))
- trData += ' colspan="' + $(cell).attr('colspan') + '"';
- if ($(cell).is("[rowspan]"))
- trData += ' rowspan="' + $(cell).attr('rowspan') + '"';
- trData += '>' + parseString(cell, row, col).replace(/\n/g,'<br>') + '</td>';
- }
- });
- if (trData.length > 0)
- docData += '<tr>' + trData + '</tr>';
- rowIndex++;
- });
- if (defaults.displayTableName)
- docData += '<tr><td></td></tr><tr><td></td></tr><tr><td>' + parseString($('<p>' + defaults.tableName + '</p>')) + '</td></tr>';
- docData += '</tbody></table>';
- if (defaults.consoleLog === true)
- console.log(docData);
- });
- var docFile = '<html xmlns:o="urn:schemas-microsoft-com:office:office" ' + MSDocSchema + ' xmlns="http://www.w3.org/TR/REC-html40">';
- docFile += '<meta http-equiv="content-type" content="application/vnd.ms-' + MSDocType + '; charset=UTF-8">';
- docFile += "<head>";
- if (MSDocType === 'excel') {
- docFile += "<!--[if gte mso 9]>";
- docFile += "<xml>";
- docFile += "<x:ExcelWorkbook>";
- docFile += "<x:ExcelWorksheets>";
- docFile += "<x:ExcelWorksheet>";
- docFile += "<x:Name>";
- docFile += defaults.worksheetName;
- docFile += "</x:Name>";
- docFile += "<x:WorksheetOptions>";
- docFile += "<x:DisplayGridlines/>";
- docFile += "</x:WorksheetOptions>";
- docFile += "</x:ExcelWorksheet>";
- docFile += "</x:ExcelWorksheets>";
- docFile += "</x:ExcelWorkbook>";
- docFile += "</xml>";
- docFile += "<![endif]-->";
- }
- docFile += "<style>br {mso-data-placement:same-cell;}</style>";
- docFile += "</head>";
- docFile += "<body>";
- docFile += docData;
- docFile += "</body>";
- docFile += "</html>";
- if (defaults.consoleLog === true)
- console.log(docFile);
- if (defaults.outputMode === 'string')
- return docFile;
- if (defaults.outputMode === 'base64')
- return base64encode(docFile);
- try {
- blob = new Blob([docFile], {type: 'application/vnd.ms-' + defaults.type});
- saveAs(blob, defaults.fileName + '.' + MSDocExt);
- }
- catch (e) {
- downloadFile(defaults.fileName + '.' + MSDocExt,
- 'data:application/vnd.ms-' + MSDocType + ';base64,',
- docFile);
- }
- } else if (defaults.type == 'xlsx') {
- var data = [];
- var ranges = [];
- rowIndex = 0;
- $rows = $(el).find('thead').first().find(defaults.theadSelector);
- $(el).find('tbody').each(function() {
- $rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
- });
- if (defaults.tfootSelector.length)
- $rows.push.apply ($rows, $(el).find('tfoot').find(defaults.tfootSelector));
- $($rows).each(function () {
- var cols = [];
- ForEachVisibleCell(this, 'th,td', rowIndex, $rows.length,
- function (cell, row, col) {
- if (typeof cell !== 'undefined' && cell !== null) {
- var colspan = parseInt(cell.getAttribute('colspan'));
- var rowspan = parseInt(cell.getAttribute('rowspan'));
- var cellValue = parseString(cell, row, col);
- if(cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;
- //Skip ranges
- ranges.forEach(function(range) {
- if(rowIndex >= range.s.r && rowIndex <= range.e.r && cols.length >= range.s.c && cols.length <= range.e.c) {
- for(var i = 0; i <= range.e.c - range.s.c; ++i) cols.push(null);
- }
- });
- //Handle Row Span
- if (rowspan || colspan) {
- rowspan = rowspan || 1;
- colspan = colspan || 1;
- ranges.push({s:{r:rowIndex, c:cols.length},e:{r:rowIndex+rowspan-1, c:cols.length+colspan-1}});
- }
- //Handle Value
- cols.push(cellValue !== "" ? cellValue : null);
- //Handle Colspan
- if (colspan) for (var k = 0; k < colspan - 1; ++k) cols.push(null);
- }
- });
- data.push(cols);
- rowIndex++;
- });
- var wb = new jx_Workbook(),
- ws = jx_createSheet(data);
- // add ranges to worksheet
- ws['!merges'] = ranges;
- // add worksheet to workbook
- wb.SheetNames.push(defaults.worksheetName);
- wb.Sheets[defaults.worksheetName] = ws;
- var wbout = XLSX.write(wb, {bookType: defaults.type, bookSST: false, type: 'binary'});
- try {
- blob = new Blob([jx_s2ab(wbout)], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8'});
- saveAs(blob, defaults.fileName + '.' + defaults.type);
- }
- catch (e) {
- downloadFile(defaults.fileName + '.' + defaults.type,
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8',
- data);
- }
- } else if (defaults.type == 'png') {
- //html2canvas($(el)[0], {
- // onrendered: function (canvas) {
- html2canvas($(el)[0]).then(
- function (canvas) {
- var image = canvas.toDataURL();
- var byteString = atob(image.substring(22)); // remove data stuff
- var buffer = new ArrayBuffer(byteString.length);
- var intArray = new Uint8Array(buffer);
- for (var i = 0; i < byteString.length; i++)
- intArray[i] = byteString.charCodeAt(i);
- if (defaults.consoleLog === true)
- console.log(byteString);
- if (defaults.outputMode === 'string')
- return byteString;
- if (defaults.outputMode === 'base64')
- return base64encode(image);
- if (defaults.outputMode === 'window') {
- window.open(image);
- return;
- }
- try {
- blob = new Blob([buffer], {type: "image/png"});
- saveAs(blob, defaults.fileName + '.png');
- }
- catch (e) {
- downloadFile(defaults.fileName + '.png',
- 'data:image/png,',
- image);
- }
- //}
- });
- } else if (defaults.type == 'pdf') {
- if (defaults.pdfmake.enabled === true) {
- // pdf output using pdfmake
- // https://github.com/bpampuch/pdfmake
- var widths = [];
- var body = [];
- rowIndex = 0;
- $hrows = $(this).find('thead').first().find(defaults.theadSelector);
- $hrows.each(function () {
- var h = [];
- ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
- function (cell, row, col) {
- h.push(parseString(cell, row, col));
- });
- if (h.length)
- body.push(h);
- for(var i = widths.length; i < h.length;i++)
- widths.push("*");
- rowIndex++;
- });
- $(this).find('tbody').each(function() {
- $rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
- });
- if (defaults.tfootSelector.length)
- $rows.push.apply ($rows, $(this).find('tfoot').find(defaults.tfootSelector));
- $($rows).each(function () {
- var r = [];
- ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
- function (cell, row, col) {
- r.push(parseString(cell, row, col));
- });
- if (r.length)
- body.push(r);
- rowIndex++;
- });
- var docDefinition = {
- pageOrientation: 'landscape',
- content: [
- {
- table: {
- headerRows: $hrows.length,
- widths: widths,
- body: body
- }
- }
- ]
- };
- pdfMake.createPdf(docDefinition).getBuffer(function (buffer) {
- try {
- var blob = new Blob([buffer], {type: "application/pdf"});
- saveAs(blob, defaults.fileName + '.pdf');
- }
- catch (e) {
- downloadFile(defaults.fileName + '.pdf',
- 'data:application/pdf;base64,',
- buffer);
- }
- });
- }
- else if (defaults.jspdf.autotable === false) {
- // pdf output using jsPDF's core html support
- var addHtmlOptions = {
- dim: {
- w: getPropertyUnitValue($(el).first().get(0), 'width', 'mm'),
- h: getPropertyUnitValue($(el).first().get(0), 'height', 'mm')
- },
- pagesplit: false
- };
- var doc = new jsPDF(defaults.jspdf.orientation, defaults.jspdf.unit, defaults.jspdf.format);
- doc.addHTML($(el).first(),
- defaults.jspdf.margins.left,
- defaults.jspdf.margins.top,
- addHtmlOptions,
- function () {
- jsPdfOutput(doc);
- });
- //delete doc;
- }
- else {
- // pdf output using jsPDF AutoTable plugin
- // https://github.com/simonbengtsson/jsPDF-AutoTable
- var teOptions = defaults.jspdf.autotable.tableExport;
- // When setting jspdf.format to 'bestfit' tableExport tries to choose
- // the minimum required paper format and orientation in which the table
- // (or tables in multitable mode) completely fits without column adjustment
- if (typeof defaults.jspdf.format === 'string' && defaults.jspdf.format.toLowerCase() === 'bestfit') {
- var pageFormats = {
- '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]
- };
- var rk = '', ro = '';
- var mw = 0;
- $(el).filter(':visible').each(function () {
- if ($(this).css('display') != 'none') {
- var w = getPropertyUnitValue($(this).get(0), 'width', 'pt');
- if (w > mw) {
- if (w > pageFormats.a0[0]) {
- rk = 'a0';
- ro = 'l';
- }
- for (var key in pageFormats) {
- if (pageFormats.hasOwnProperty(key)) {
- if (pageFormats[key][1] > w) {
- rk = key;
- ro = 'l';
- if (pageFormats[key][0] > w)
- ro = 'p';
- }
- }
- }
- mw = w;
- }
- }
- });
- defaults.jspdf.format = (rk === '' ? 'a4' : rk);
- defaults.jspdf.orientation = (ro === '' ? 'w' : ro);
- }
- // The jsPDF doc object is stored in defaults.jspdf.autotable.tableExport,
- // thus it can be accessed from any callback function
- teOptions.doc = new jsPDF(defaults.jspdf.orientation,
- defaults.jspdf.unit,
- defaults.jspdf.format);
- if (teOptions.outputImages === true)
- teOptions.images = {};
- if (typeof teOptions.images != 'undefined') {
- $(el).filter(function() {
- return $(this).data("tableexport-display") != 'none' &&
- ($(this).is(':visible') ||
- $(this).data("tableexport-display") == 'always');
- }).each(function () {
- var rowCount = 0;
- $hrows = $(this).find('thead').find(defaults.theadSelector);
- $(this).find('tbody').each(function() {
- $rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
- });
- if (defaults.tfootSelector.length)
- $rows.push.apply ($rows, $(this).find('tfoot').find(defaults.tfootSelector));
- $($rows).each(function () {
- ForEachVisibleCell(this, 'td,th', $hrows.length + rowCount, $hrows.length + $rows.length,
- function (cell, row, col) {
- if (typeof cell !== 'undefined' && cell !== null) {
- var kids = $(cell).children();
- if (typeof kids != 'undefined' && kids.length > 0)
- collectImages (cell, kids, teOptions);
- }
- });
- rowCount++;
- });
- });
- $hrows = [];
- $rows = [];
- }
- loadImages ( teOptions, function (imageCount) {
- $(el).filter(function() {
- return $(this).data("tableexport-display") != 'none' &&
- ($(this).is(':visible') ||
- $(this).data("tableexport-display") == 'always');
- }).each(function () {
- var colKey;
- var rowIndex = 0;
- colNames = GetColumnNames (this);
- teOptions.columns = [];
- teOptions.rows = [];
- teOptions.rowoptions = {};
- // onTable: optional callback function for every matching table that can be used
- // to modify the tableExport options or to skip the output of a particular table
- // if the table selector targets multiple tables
- if (typeof teOptions.onTable === 'function')
- if (teOptions.onTable($(this), defaults) === false)
- return true; // continue to next iteration step (table)
- // each table works with an own copy of AutoTable options
- defaults.jspdf.autotable.tableExport = null; // avoid deep recursion error
- var atOptions = $.extend(true, {}, defaults.jspdf.autotable);
- defaults.jspdf.autotable.tableExport = teOptions;
- atOptions.margin = {};
- $.extend(true, atOptions.margin, defaults.jspdf.margins);
- atOptions.tableExport = teOptions;
- // Fix jsPDF Autotable's row height calculation
- if (typeof atOptions.beforePageContent !== 'function') {
- atOptions.beforePageContent = function (data) {
- if (data.pageCount == 1) {
- var all = data.table.rows.concat(data.table.headerRow);
- all.forEach(function (row) {
- if ( row.height > 0 ) {
- row.height += (2 - FONT_ROW_RATIO) / 2 * row.styles.fontSize;
- data.table.height += (2 - FONT_ROW_RATIO) / 2 * row.styles.fontSize;
- }
- });
- }
- };
- }
- if (typeof atOptions.createdHeaderCell !== 'function') {
- // apply some original css styles to pdf header cells
- atOptions.createdHeaderCell = function (cell, data) {
- // jsPDF AutoTable plugin v2.0.14 fix: each cell needs its own styles object
- cell.styles = $.extend({}, data.row.styles);
- if (typeof teOptions.columns [data.column.dataKey] != 'undefined') {
- var col = teOptions.columns [data.column.dataKey];
- if (typeof col.rect != 'undefined') {
- var rh;
- cell.contentWidth = col.rect.width;
- if (typeof teOptions.heightRatio == 'undefined' || teOptions.heightRatio === 0) {
- if (data.row.raw [data.column.dataKey].rowspan)
- rh = data.row.raw [data.column.dataKey].rect.height / data.row.raw [data.column.dataKey].rowspan;
- else
- rh = data.row.raw [data.column.dataKey].rect.height;
- teOptions.heightRatio = cell.styles.rowHeight / rh;
- }
- rh = data.row.raw [data.column.dataKey].rect.height * teOptions.heightRatio;
- if (rh > cell.styles.rowHeight)
- cell.styles.rowHeight = rh;
- }
- if (typeof col.style != 'undefined' && col.style.hidden !== true) {
- cell.styles.halign = col.style.align;
- if (atOptions.styles.fillColor === 'inherit')
- cell.styles.fillColor = col.style.bcolor;
- if (atOptions.styles.textColor === 'inherit')
- cell.styles.textColor = col.style.color;
- if (atOptions.styles.fontStyle === 'inherit')
- cell.styles.fontStyle = col.style.fstyle;
- }
- }
- };
- }
- if (typeof atOptions.createdCell !== 'function') {
- // apply some original css styles to pdf table cells
- atOptions.createdCell = function (cell, data) {
- var rowopt = teOptions.rowoptions [data.row.index + ":" + data.column.dataKey];
- if (typeof rowopt != 'undefined' &&
- typeof rowopt.style != 'undefined' &&
- rowopt.style.hidden !== true) {
- cell.styles.halign = rowopt.style.align;
- if (atOptions.styles.fillColor === 'inherit')
- cell.styles.fillColor = rowopt.style.bcolor;
- if (atOptions.styles.textColor === 'inherit')
- cell.styles.textColor = rowopt.style.color;
- if (atOptions.styles.fontStyle === 'inherit')
- cell.styles.fontStyle = rowopt.style.fstyle;
- }
- };
- }
- if (typeof atOptions.drawHeaderCell !== 'function') {
- atOptions.drawHeaderCell = function (cell, data) {
- var colopt = teOptions.columns [data.column.dataKey];
- if ((colopt.style.hasOwnProperty("hidden") !== true || colopt.style.hidden !== true) &&
- colopt.rowIndex >= 0 )
- return prepareAutoTableText (cell, data, colopt);
- else
- return false; // cell is hidden
- };
- }
- if (typeof atOptions.drawCell !== 'function') {
- atOptions.drawCell = function (cell, data) {
- var rowopt = teOptions.rowoptions [data.row.index + ":" + data.column.dataKey];
- if ( prepareAutoTableText (cell, data, rowopt) ) {
- teOptions.doc.rect(cell.x, cell.y, cell.width, cell.height, cell.styles.fillStyle);
- if (typeof rowopt != 'undefined' && typeof rowopt.kids != 'undefined' && rowopt.kids.length > 0) {
- var dh = cell.height / rowopt.rect.height;
- if ( dh > teOptions.dh || typeof teOptions.dh == 'undefined' )
- teOptions.dh = dh;
- teOptions.dw = cell.width / rowopt.rect.width;
- drawCellElements (cell, rowopt.kids, teOptions);
- }
- teOptions.doc.autoTableText(cell.text, cell.textPos.x, cell.textPos.y, {
- halign: cell.styles.halign,
- valign: cell.styles.valign
- });
- }
- return false;
- };
- }
- // collect header and data rows
- teOptions.headerrows = [];
- $hrows = $(this).find('thead').find(defaults.theadSelector);
- $hrows.each(function () {
- colKey = 0;
- teOptions.headerrows[rowIndex] = [];
- ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
- function (cell, row, col) {
- var obj = getCellStyles (cell);
- obj.title = parseString(cell, row, col);
- obj.key = colKey++;
- obj.rowIndex = rowIndex;
- teOptions.headerrows[rowIndex].push(obj);
- });
- rowIndex++;
- });
- if (rowIndex > 0) {
- // iterate through last row
- $.each(teOptions.headerrows[rowIndex-1], function () {
- var obj = this;
- if (rowIndex > 1 && this.rect === null)
- obj = teOptions.headerrows[rowIndex-2][this.key];
- if (obj !== null)
- teOptions.columns.push(obj);
- });
- }
- var rowCount = 0;
- $rows = [];
- $(this).find('tbody').each(function() {
- $rows.push.apply ($rows, $(this).find(defaults.tbodySelector));
- });
- if (defaults.tfootSelector.length)
- $rows.push.apply ($rows, $(this).find('tfoot').find(defaults.tfootSelector));
- $($rows).each(function () {
- var rowData = [];
- colKey = 0;
- ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
- function (cell, row, col) {
- if (typeof teOptions.columns[colKey] === 'undefined') {
- // jsPDF-Autotable needs columns. Thus define hidden ones for tables without thead
- var obj = {
- title: '',
- key: colKey,
- style: {
- hidden: true
- }
- };
- teOptions.columns.push(obj);
- }
- if (typeof cell !== 'undefined' && cell !== null) {
- var obj = getCellStyles (cell);
- obj.kids = $(cell).children();
- teOptions.rowoptions [rowCount + ":" + colKey++] = obj;
- }
- else {
- var obj = $.extend(true, {}, teOptions.rowoptions [rowCount + ":" + (colKey-1)]);
- obj.colspan = -1;
- teOptions.rowoptions [rowCount + ":" + colKey++] = obj;
- }
- rowData.push(parseString(cell, row, col));
- });
- if (rowData.length) {
- teOptions.rows.push(rowData);
- rowCount++;
- }
- rowIndex++;
- });
- // onBeforeAutotable: optional callback function before calling
- // jsPDF AutoTable that can be used to modify the AutoTable options
- if (typeof teOptions.onBeforeAutotable === 'function')
- teOptions.onBeforeAutotable($(this), teOptions.columns, teOptions.rows, atOptions);
- teOptions.doc.autoTable(teOptions.columns, teOptions.rows, atOptions);
- // onAfterAutotable: optional callback function after returning
- // from jsPDF AutoTable that can be used to modify the AutoTable options
- if (typeof teOptions.onAfterAutotable === 'function')
- teOptions.onAfterAutotable($(this), atOptions);
- // set the start position for the next table (in case there is one)
- defaults.jspdf.autotable.startY = teOptions.doc.autoTableEndPosY() + atOptions.margin.top;
- });
- jsPdfOutput(teOptions.doc);
- if (typeof teOptions.headerrows != 'undefined')
- teOptions.headerrows.length = 0;
- if (typeof teOptions.columns != 'undefined')
- teOptions.columns.length = 0;
- if (typeof teOptions.rows != 'undefined')
- teOptions.rows.length = 0;
- delete teOptions.doc;
- teOptions.doc = null;
- });
- }
- }
- function FindColObject (objects, colIndex, rowIndex) {
- var result = null;
- $.each(objects, function () {
- if (this.rowIndex == rowIndex && this.key == colIndex) {
- result = this;
- return false;
- }
- });
- return result;
- }
- function GetColumnNames (table) {
- var result = [];
- $(table).find('thead').first().find('th').each(function(index, el) {
- if ($(el).attr("data-field") !== undefined)
- result[index] = $(el).attr("data-field");
- else
- result[index] = index.toString();
- });
- return result;
- }
- function isColumnIgnored($row, colIndex) {
- var result = false;
- if (defaults.ignoreColumn.length > 0) {
- if (typeof defaults.ignoreColumn[0] == 'string') {
- if (colNames.length > colIndex && typeof colNames[colIndex] != 'undefined')
- if ($.inArray(colNames[colIndex], defaults.ignoreColumn) != -1)
- result = true;
- }
- else if (typeof defaults.ignoreColumn[0] == 'number') {
- if ($.inArray(colIndex, defaults.ignoreColumn) != -1 ||
- $.inArray(colIndex-$row.length, defaults.ignoreColumn) != -1)
- result = true;
- }
- }
- return result;
- }
- function ForEachVisibleCell(tableRow, selector, rowIndex, rowCount, cellcallback) {
- if ($.inArray(rowIndex, defaults.ignoreRow) == -1 &&
- $.inArray(rowIndex-rowCount, defaults.ignoreRow) == -1) {
- var $row = $(tableRow).filter(function() {
- return $(this).data("tableexport-display") != 'none' &&
- ($(this).is(':visible') ||
- $(this).data("tableexport-display") == 'always' ||
- $(this).closest('table').data("tableexport-display") == 'always');
- }).find(selector);
- var rowColspan = 0;
- var rowColIndex = 0;
- $row.each(function (colIndex) {
- if ($(this).data("tableexport-display") == 'always' ||
- ($(this).css('display') != 'none' &&
- $(this).css('visibility') != 'hidden' &&
- $(this).data("tableexport-display") != 'none')) {
- if (isColumnIgnored($row, colIndex) === false) {
- if (typeof (cellcallback) === "function") {
- var c, Colspan = 0;
- var r, Rowspan = 0;
- // handle rowspans from previous rows
- if (typeof rowspans[rowIndex] != 'undefined' && rowspans[rowIndex].length > 0) {
- for (c = 0; c <= colIndex; c++) {
- if (typeof rowspans[rowIndex][c] != 'undefined') {
- cellcallback(null, rowIndex, c);
- delete rowspans[rowIndex][c];
- colIndex++;
- }
- }
- }
- rowColIndex = colIndex;
- if ($(this).is("[colspan]")) {
- Colspan = parseInt($(this).attr('colspan'));
- rowColspan += Colspan > 0 ? Colspan - 1 : 0;
- }
- if ($(this).is("[rowspan]"))
- Rowspan = parseInt($(this).attr('rowspan'));
- // output content of current cell
- cellcallback(this, rowIndex, colIndex);
- // handle colspan of current cell
- for (c = 0; c < Colspan - 1; c++)
- cellcallback(null, rowIndex, colIndex + c);
- // store rowspan for following rows
- if (Rowspan) {
- for (r = 1; r < Rowspan; r++) {
- if (typeof rowspans[rowIndex + r] == 'undefined')
- rowspans[rowIndex + r] = [];
- rowspans[rowIndex + r][colIndex + rowColspan] = "";
- for (c = 1; c < Colspan; c++)
- rowspans[rowIndex + r][colIndex + rowColspan - c] = "";
- }
- }
- }
- }
- }
- });
- // handle rowspans from previous rows
- if (typeof rowspans[rowIndex] != 'undefined' && rowspans[rowIndex].length > 0) {
- for (var c = 0; c <= rowspans[rowIndex].length; c++) {
- if (typeof rowspans[rowIndex][c] != 'undefined') {
- cellcallback(null, rowIndex, c);
- delete rowspans[rowIndex][c];
- }
- }
- }
- }
- }
- function jsPdfOutput(doc) {
- if (defaults.consoleLog === true)
- console.log(doc.output());
- if (defaults.outputMode === 'string')
- return doc.output();
- if (defaults.outputMode === 'base64')
- return base64encode(doc.output());
- if (defaults.outputMode === 'window') {
- window.open(URL.createObjectURL(doc.output("blob")));
- return;
- }
- try {
- var blob = doc.output('blob');
- saveAs(blob, defaults.fileName + '.pdf');
- }
- catch (e) {
- downloadFile(defaults.fileName + '.pdf',
- 'data:application/pdf;base64,',
- doc.output());
- }
- }
- function prepareAutoTableText (cell, data, cellopt) {
- var cs = 0;
- if ( typeof cellopt != 'undefined' )
- cs = cellopt.colspan;
- if ( cs >= 0 ) {
- // colspan handling
- var cellWidth = cell.width;
- var textPosX = cell.textPos.x;
- var i = data.table.columns.indexOf(data.column);
- for (var c = 1; c < cs; c++) {
- var column = data.table.columns[i+c];
- cellWidth += column.width;
- }
- if ( cs > 1 ) {
- if ( cell.styles.halign === 'right' )
- textPosX = cell.textPos.x + cellWidth - cell.width;
- else if ( cell.styles.halign === 'center' )
- textPosX = cell.textPos.x + (cellWidth - cell.width) / 2;
- }
- cell.width = cellWidth;
- cell.textPos.x = textPosX;
- if ( typeof cellopt != 'undefined' && cellopt.rowspan > 1 )
- cell.height = cell.height * cellopt.rowspan;
- // fix jsPDF's calculation of text position
- if ( cell.styles.valign === 'middle' || cell.styles.valign === 'bottom' ) {
- var splittedText = typeof cell.text === 'string' ? cell.text.split(/\r\n|\r|\n/g) : cell.text;
- var lineCount = splittedText.length || 1;
- if (lineCount > 2)
- cell.textPos.y -= ((2 - FONT_ROW_RATIO) / 2 * data.row.styles.fontSize) * (lineCount-2) / 3 ;
- }
- return true;
- }
- else
- return false; // cell is hidden (colspan = -1), don't draw it
- }
- function collectImages (cell, elements, teOptions) {
- if (typeof teOptions.images != 'undefined') {
- elements.each(function () {
- var kids = $(this).children();
- if ( $(this).is("img") ) {
- var hash = strHashCode(this.src);
- teOptions.images[hash] = { url: this.src,
- src: this.src };
- }
- if (typeof kids != 'undefined' && kids.length > 0)
- collectImages (cell, kids, teOptions);
- });
- }
- }
- function loadImages (teOptions, callback) {
- var i;
- var imageCount = 0;
- var x = 0;
- function done() {
- callback(imageCount);
- }
- function loadImage(image) {
- if (!image.url)
- return;
- var img = new Image();
- imageCount = ++x;
- img.crossOrigin = 'Anonymous';
- img.onerror = img.onload = function () {
- if(img.complete) {
- if (img.src.indexOf('data:image/') === 0) {
- img.width = image.width || img.width || 0;
- img.height = image.height || img.height || 0;
- }
- if (img.width + img.height) {
- var canvas = document.createElement("canvas");
- var ctx = canvas.getContext("2d");
- canvas.width = img.width;
- canvas.height = img.height;
- ctx.drawImage( img, 0, 0 );
- image.src = canvas.toDataURL("image/jpeg");
- }
- }
- if(!--x)
- done();
- };
- img.src = image.url;
- }
- if (typeof teOptions.images != 'undefined') {
- for (i in teOptions.images)
- if (teOptions.images.hasOwnProperty(i))
- loadImage(teOptions.images[i]);
- }
- return x || done();
- }
- function drawCellElements (cell, elements, teOptions) {
- elements.each(function () {
- var kids = $(this).children();
- if ( $(this).is("div") ) {
- var bcolor = rgb2array(getStyle(this, 'background-color'), [255, 255, 255]);
- var lcolor = rgb2array(getStyle(this, 'border-top-color'), [0, 0, 0]);
- var lwidth = getPropertyUnitValue(this, 'border-top-width', defaults.jspdf.unit);
- var r = this.getBoundingClientRect();
- var ux = this.offsetLeft * teOptions.dw;
- var uy = this.offsetTop * teOptions.dh;
- var uw = r.width * teOptions.dw;
- var uh = r.height * teOptions.dh;
- teOptions.doc.setDrawColor.apply (undefined, lcolor);
- teOptions.doc.setFillColor.apply (undefined, bcolor);
- teOptions.doc.setLineWidth (lwidth);
- teOptions.doc.rect(cell.x + ux, cell.y + uy, uw, uh, lwidth ? "FD" : "F");
- }
- else if ( $(this).is("img") ) {
- if (typeof teOptions.images != 'undefined') {
- var hash = strHashCode(this.src);
- var image = teOptions.images[hash];
- if (typeof image != 'undefined') {
- var arCell = cell.width / cell.height;
- var arImg = this.width / this.height;
- var imgWidth = cell.width;
- var imgHeight = cell.height;
- var uy = 0;
- if (arImg < arCell) {
- imgHeight = Math.min (cell.height, this.height);
- imgWidth = this.width * imgHeight / this.height;
- }
- else if (arImg > arCell) {
- imgWidth = Math.min (cell.width, this.width);
- imgHeight = this.height * imgWidth / this.width;
- }
- if (imgHeight < cell.height)
- uy = (cell.height - imgHeight) / 2;
- teOptions.doc.addImage (image.src, cell.textPos.x, cell.y + uy, imgWidth, imgHeight);
- cell.textPos.x += imgWidth;
- }
- }
- }
- if (typeof kids != 'undefined' && kids.length > 0)
- drawCellElements (cell, kids, teOptions);
- });
- }
- function escapeRegExp(string) {
- return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
- }
- function replaceAll(string, find, replace) {
- return string.replace(new RegExp(escapeRegExp(find), 'g'), replace);
- }
- // Takes a string and encapsulates it (by default in double-quotes) if it
- // contains the csv field separator, spaces, or linebreaks.
- function csvString(cell, rowIndex, colIndex) {
- var result = '';
- if (cell !== null) {
- var dataString = parseString(cell, rowIndex, colIndex);
- var csvValue = (dataString === null || dataString === '') ? '' : dataString.toString();
- if (dataString instanceof Date)
- result = defaults.csvEnclosure + dataString.toLocaleString() + defaults.csvEnclosure;
- else {
- result = replaceAll(csvValue, defaults.csvEnclosure, defaults.csvEnclosure + defaults.csvEnclosure);
- if (result.indexOf(defaults.csvSeparator) >= 0 || /[\r\n ]/g.test(result))
- result = defaults.csvEnclosure + result + defaults.csvEnclosure;
- }
- }
- return result;
- }
- function parseNumber(value) {
- value = value || "0";
- value = replaceAll(value, defaults.numbers.html.decimalMark, '.');
- value = replaceAll(value, defaults.numbers.html.thousandsSeparator, '');
- return typeof value === "number" || jQuery.isNumeric(value) !== false ? value : false;
- }
- function parseString(cell, rowIndex, colIndex) {
- var result = '';
- if (cell !== null) {
- var $cell = $(cell);
- var htmlData;
- if ($cell[0].hasAttribute("data-tableexport-value"))
- htmlData = $cell.data("tableexport-value");
- else
- htmlData = $cell.html();
- if (htmlData != '') {
- var html = $.parseHTML( htmlData );
- htmlData = '';
- $.each( html, function() {
- if ( $(this).is("input") )
- htmlData += $cell.find('input').val();
- else if ( $(this).is("select") )
- htmlData += $cell.find('select option:selected').text();
- else {
- htmlData += $cell.html();
- return false;
- }
- });
- }
- if (typeof defaults.onCellHtmlData === 'function')
- htmlData = defaults.onCellHtmlData($cell, rowIndex, colIndex, htmlData);
- if (defaults.htmlContent === true) {
- result = $.trim(htmlData);
- }
- else {
- var text = htmlData.replace(/\n/g,'\u2028').replace(/<br\s*[\/]?>/gi, '\u2060');
- var obj = $('<div/>').html(text).contents();
- text = '';
- $.each(obj.text().split("\u2028"), function(i, v) {
- if (i > 0)
- text += " ";
- text += $.trim(v);
- });
- $.each(text.split("\u2060"), function(i, v) {
- if (i > 0)
- result += "\n";
- result += $.trim(v).replace(/\u00AD/g, ""); // remove soft hyphens
- });
- if (defaults.numbers.html.decimalMark != defaults.numbers.output.decimalMark ||
- defaults.numbers.html.thousandsSeparator != defaults.numbers.output.thousandsSeparator) {
- var number = parseNumber (result);
- if ( number !== false ) {
- var frac = ("" + number).split('.');
- if ( frac.length == 1 )
- frac[1] = "";
- var mod = frac[0].length > 3 ? frac[0].length % 3 : 0;
- result = (number < 0 ? "-" : "") +
- (defaults.numbers.output.thousandsSeparator ? ((mod ? frac[0].substr(0, mod) + defaults.numbers.output.thousandsSeparator : "") + frac[0].substr(mod).replace(/(\d{3})(?=\d)/g, "$1" + defaults.numbers.output.thousandsSeparator)) : frac[0]) +
- (frac[1].length ? defaults.numbers.output.decimalMark + frac[1] : "");
- }
- }
- }
- if (defaults.escape === true) {
- result = escape(result);
- }
- if (typeof defaults.onCellData === 'function') {
- result = defaults.onCellData($cell, rowIndex, colIndex, result);
- }
- }
- return result;
- }
- function hyphenate(a, b, c) {
- return b + "-" + c.toLowerCase();
- }
- function rgb2array(rgb_string, default_result) {
- var re = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/;
- var bits = re.exec(rgb_string);
- var result = default_result;
- if (bits)
- result = [ parseInt(bits[1]), parseInt(bits[2]), parseInt(bits[3]) ];
- return result;
- }
- function getCellStyles (cell) {
- var a = getStyle(cell, 'text-align');
- var fw = getStyle(cell, 'font-weight');
- var fs = getStyle(cell, 'font-style');
- var f = '';
- if (a == 'start')
- a = getStyle(cell, 'direction') == 'rtl' ? 'right' : 'left';
- if (fw >= 700)
- f = 'bold';
- if (fs == 'italic')
- f += fs;
- if (f === '')
- f = 'normal';
- var result = {
- style: {
- align: a,
- bcolor: rgb2array(getStyle(cell, 'background-color'), [255, 255, 255]),
- color: rgb2array(getStyle(cell, 'color'), [0, 0, 0]),
- fstyle: f
- },
- colspan: (parseInt($(cell).attr('colspan')) || 0),
- rowspan: (parseInt($(cell).attr('rowspan')) || 0)
- };
- if (cell !== null) {
- var r = cell.getBoundingClientRect();
- result.rect = {
- width: r.width,
- height: r.height
- };
- }
- return result;
- }
- // get computed style property
- function getStyle(target, prop) {
- try {
- if (window.getComputedStyle) { // gecko and webkit
- prop = prop.replace(/([a-z])([A-Z])/, hyphenate); // requires hyphenated, not camel
- return window.getComputedStyle(target, null).getPropertyValue(prop);
- }
- if (target.currentStyle) { // ie
- return target.currentStyle[prop];
- }
- return target.style[prop];
- }
- catch (e) {
- }
- return "";
- }
- function getUnitValue(parent, value, unit) {
- var baseline = 100; // any number serves
- var temp = document.createElement("div"); // create temporary element
- temp.style.overflow = "hidden"; // in case baseline is set too low
- temp.style.visibility = "hidden"; // no need to show it
- parent.appendChild(temp); // insert it into the parent for em, ex and %
- temp.style.width = baseline + unit;
- var factor = baseline / temp.offsetWidth;
- parent.removeChild(temp); // clean up
- return (value * factor);
- }
- function getPropertyUnitValue(target, prop, unit) {
- var value = getStyle(target, prop); // get the computed style value
- var numeric = value.match(/\d+/); // get the numeric component
- if (numeric !== null) {
- numeric = numeric[0]; // get the string
- return getUnitValue (target.parentElement, numeric, unit);
- }
- return 0;
- }
- function jx_Workbook() {
- if(!(this instanceof jx_Workbook)) return new jx_Workbook();
- this.SheetNames = [];
- this.Sheets = {};
- }
- function jx_s2ab(s) {
- var buf = new ArrayBuffer(s.length);
- var view = new Uint8Array(buf);
- for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
- return buf;
- }
- function jx_datenum(v, date1904) {
- if(date1904) v+=1462;
- var epoch = Date.parse(v);
- return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
- }
- function jx_createSheet(data) {
- var ws = {};
- var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
- for(var R = 0; R != data.length; ++R) {
- for(var C = 0; C != data[R].length; ++C) {
- if(range.s.r > R) range.s.r = R;
- if(range.s.c > C) range.s.c = C;
- if(range.e.r < R) range.e.r = R;
- if(range.e.c < C) range.e.c = C;
- var cell = {v: data[R][C] };
- if(cell.v === null) continue;
- var cell_ref = XLSX.utils.encode_cell({c:C,r:R});
- if(typeof cell.v === 'number') cell.t = 'n';
- else if(typeof cell.v === 'boolean') cell.t = 'b';
- else if(cell.v instanceof Date) {
- cell.t = 'n'; cell.z = XLSX.SSF._table[14];
- cell.v = jx_datenum(cell.v);
- }
- else cell.t = 's';
- ws[cell_ref] = cell;
- }
- }
- if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
- return ws;
- }
- function strHashCode (str) {
- var hash = 0, i, chr, len;
- if (str.length === 0) return hash;
- for (i = 0, len = str.length; i < len; i++) {
- chr = str.charCodeAt(i);
- hash = ((hash << 5) - hash) + chr;
- hash |= 0; // Convert to 32bit integer
- }
- return hash;
- }
- function downloadFile(filename, header, data) {
- var ua = window.navigator.userAgent;
- if (filename !== false && (ua.indexOf("MSIE ") > 0 || !!ua.match(/Trident.*rv\:11\./))) {
- if (window.navigator.msSaveOrOpenBlob)
- window.navigator.msSaveOrOpenBlob(new Blob([data]), filename);
- else {
- // Internet Explorer (<= 9) workaround by Darryl (https://github.com/dawiong/tableExport.jquery.plugin)
- // based on sampopes answer on http://stackoverflow.com/questions/22317951
- // ! Not working for json and pdf format !
- var frame = document.createElement("iframe");
- if (frame) {
- document.body.appendChild(frame);
- frame.setAttribute("style", "display:none");
- frame.contentDocument.open("txt/html", "replace");
- frame.contentDocument.write(data);
- frame.contentDocument.close();
- frame.focus();
- frame.contentDocument.execCommand("SaveAs", true, filename);
- document.body.removeChild(frame);
- }
- }
- }
- else {
- var DownloadLink = document.createElement('a');
- if (DownloadLink) {
- DownloadLink.style.display = 'none';
- if (filename !== false)
- DownloadLink.download = filename;
- else
- DownloadLink.target = '_blank';
- if (header.toLowerCase().indexOf("base64,") >= 0)
- DownloadLink.href = header + base64encode(data);
- else
- DownloadLink.href = header + encodeURIComponent(data);
- document.body.appendChild(DownloadLink);
- if (document.createEvent) {
- if (DownloadEvt === null)
- DownloadEvt = document.createEvent('MouseEvents');
- DownloadEvt.initEvent('click', true, false);
- DownloadLink.dispatchEvent(DownloadEvt);
- }
- else if (document.createEventObject)
- DownloadLink.fireEvent('onclick');
- else if (typeof DownloadLink.onclick == 'function')
- DownloadLink.onclick();
- document.body.removeChild(DownloadLink);
- }
- }
- }
- function utf8Encode(string) {
- string = string.replace(/\x0d\x0a/g, "\x0a");
- var utftext = "";
- for (var n = 0; n < string.length; n++) {
- var c = string.charCodeAt(n);
- if (c < 128) {
- utftext += String.fromCharCode(c);
- }
- else if ((c > 127) && (c < 2048)) {
- utftext += String.fromCharCode((c >> 6) | 192);
- utftext += String.fromCharCode((c & 63) | 128);
- }
- else {
- utftext += String.fromCharCode((c >> 12) | 224);
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
- utftext += String.fromCharCode((c & 63) | 128);
- }
- }
- return utftext;
- }
- function base64encode(input) {
- var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- var output = "";
- var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
- var i = 0;
- input = utf8Encode(input);
- while (i < input.length) {
- chr1 = input.charCodeAt(i++);
- chr2 = input.charCodeAt(i++);
- chr3 = input.charCodeAt(i++);
- enc1 = chr1 >> 2;
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
- enc4 = chr3 & 63;
- if (isNaN(chr2)) {
- enc3 = enc4 = 64;
- } else if (isNaN(chr3)) {
- enc4 = 64;
- }
- output = output +
- keyStr.charAt(enc1) + keyStr.charAt(enc2) +
- keyStr.charAt(enc3) + keyStr.charAt(enc4);
- }
- return output;
- }
- return this;
- }
- });
- })(jQuery);
|