diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..150fd25d --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "kayalshri/tableExport.jquery.plugin", + "description": "Export html table to json, xml, pdf, ...", + "keywords": [ + "table", + "export" + ], + "type": "component", + "homepage": "https://github.com/kayalshri/tableExport.jquery.plugin", + "authors": [ + { + "name": "kayalshri" + } + ] +} diff --git a/jspdf/libs/base64.js b/jspdf/libs/base64.js index 7d9536a4..05684b17 100644 --- a/jspdf/libs/base64.js +++ b/jspdf/libs/base64.js @@ -1,143 +1,59 @@ - -/** -* -* Base64 encode / decode -* http://www.webtoolkit.info/ -* -**/ - -var Base64 = { - - // private property - _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", - - // public method for encoding - encode : function (input) { - var output = ""; - var chr1, chr2, chr3, enc1, enc2, enc3, enc4; - var i = 0; - - input = Base64._utf8_encode(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 + - this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + - this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); - - } - - return output; - }, - - // public method for decoding - decode : function (input) { - var output = ""; - var chr1, chr2, chr3; - var enc1, enc2, enc3, enc4; - var i = 0; - - input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); - - while (i < input.length) { - - enc1 = this._keyStr.indexOf(input.charAt(i++)); - enc2 = this._keyStr.indexOf(input.charAt(i++)); - enc3 = this._keyStr.indexOf(input.charAt(i++)); - enc4 = this._keyStr.indexOf(input.charAt(i++)); - - chr1 = (enc1 << 2) | (enc2 >> 4); - chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); - chr3 = ((enc3 & 3) << 6) | enc4; - - output = output + String.fromCharCode(chr1); - - if (enc3 != 64) { - output = output + String.fromCharCode(chr2); - } - if (enc4 != 64) { - output = output + String.fromCharCode(chr3); - } - - } - - output = Base64._utf8_decode(output); - - return output; - - }, - - // private method for UTF-8 encoding - _utf8_encode : function (string) { - string = string.replace(/\r\n/g,"\n"); - 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; - }, - - // private method for UTF-8 decoding - _utf8_decode : function (utftext) { - var string = ""; - var i = 0; - var c = c1 = c2 = 0; - - while ( i < utftext.length ) { - - c = utftext.charCodeAt(i); - - if (c < 128) { - string += String.fromCharCode(c); - i++; - } - else if((c > 191) && (c < 224)) { - c2 = utftext.charCodeAt(i+1); - string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); - i += 2; - } - else { - c2 = utftext.charCodeAt(i+1); - c3 = utftext.charCodeAt(i+2); - string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); - i += 3; - } - - } - - return string; - } - -} +jQuery.base64 = (function($) { + + // private property + var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + + // private method for UTF-8 encoding + function utf8Encode(string) { + string = string.replace(/\r\n/g,"\n"); + 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 encode(input) { + 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 { + encode: function (str) { + return encode(str); + } + }; + +}(jQuery)); \ No newline at end of file diff --git a/tableExport.js b/tableExport.js old mode 100644 new mode 100755 index 1bfaa0fc..5a2bfda4 --- a/tableExport.js +++ b/tableExport.js @@ -1,359 +1,353 @@ -/*The MIT License (MIT) +(function(f,a){"function"==typeof define&&define.amd?define([],a):"object"==typeof exports?module.exports=a():f.download=a()})(this,function(){return function(f,a,b){function n(l){var a=l.split(/[:;,]/);l=a[1];var a=("base64"==a[2]?atob:decodeURIComponent)(a.pop()),c=a.length,b=0,d=new Uint8Array(c);for(b;b"; - } - } - }); - }); - xml += ''; - - // Row Vs Column - var rowCount=1; - $(el).find('tbody').find('tr').each(function() { - xml += ''; - var colCount=0; - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - xml += ""+parseString($(this))+""; - } - } - colCount++; - }); - rowCount++; - xml += ''; - }); - xml += '' - - if(defaults.consoleLog == 'true'){ - console.log(xml); - } - - var base64data = "base64," + $.base64.encode(xml); - window.open('data:application/xml;filename=exportData;' + base64data); - - }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){ - //console.log($(this).html()); - var excel=""; - // Header - $(el).find('thead').find('tr').each(function() { - excel += ""; - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += ""; - } - } - }); - excel += ''; - - }); - - - // Row Vs Column - var rowCount=1; - $(el).find('tbody').find('tr').each(function() { - excel += ""; - var colCount=0; - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += ""; - } - } - colCount++; - }); - rowCount++; - excel += ''; - }); - excel += '
" + parseString($(this))+ "
"+parseString($(this))+"
' - - if(defaults.consoleLog == 'true'){ - console.log(excel); - } - - var excelFile = ""; - excelFile += ""; - excelFile += ""; - excelFile += ""; - excelFile += ""; - excelFile += excel; - excelFile += ""; - excelFile += ""; - - var base64data = "base64," + $.base64.encode(excelFile); - window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data); - - }else if(defaults.type == 'png'){ - html2canvas($(el), { - onrendered: function(canvas) { - var img = canvas.toDataURL("image/png"); - window.open(img); - - - } - }); - }else if(defaults.type == 'pdf'){ - - var doc = new jsPDF('p','pt', 'a4', true); - doc.setFontSize(defaults.pdfFontSize); - - // Header - var startColPosition=defaults.pdfLeftMargin; - $(el).find('thead').find('tr').each(function() { - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - var colPosition = startColPosition+ (index * 50); - doc.text(colPosition,20, parseString($(this))); - } - } - }); - }); - - - // Row Vs Column - var startRowPosition = 20; var page =1;var rowPosition=0; - $(el).find('tbody').find('tr').each(function(index,data) { - rowCalc = index+1; - - if (rowCalc % 26 == 0){ - doc.addPage(); - page++; - startRowPosition=startRowPosition+10; - } - rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280); - - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - var colPosition = startColPosition+ (index * 50); - doc.text(colPosition,rowPosition, parseString($(this))); - } - } - - }); - - }); - - // Output as Data URI - doc.output('datauri'); - - } - - - function parseString(data){ - - if(defaults.htmlContent == 'true'){ - content_data = data.html().trim(); - }else{ - content_data = data.text().trim(); - } - - if(defaults.escape == 'true'){ - content_data = escape(content_data); - } - - - - return content_data; - } - - } - }); - })(jQuery); - + //output + if(defaults.consoleLog == 'true'){ + console.log(tdData); + } + var base64data = "base64," + $.base64.encode(tdData); + //window.open('data:application/'+defaults.type+';filename=exportData;' + base64data); + download('data:text/plain;'+base64data,'download.'+defaults.type,'text/plain'); + }else if(defaults.type == 'sql'){ + + // Header + var tdData ="INSERT INTO `"+defaults.tableName+"` ("; + $(el).find('thead').find('tr').each(function() { + + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + tdData += '`' + parseString($(this)) + '`,' ; + } + } + }); + tdData = $.trim(tdData); + tdData = $.trim(tdData).substring(0, tdData.length -1); + }); + tdData += ") VALUES "; + // Row vs Column + $(el).find('tbody').find('tr').each(function() { + tdData += "("; + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + tdData += '"'+ parseString($(this)) + '",'; + } + } + }); + + tdData = $.trim(tdData).substring(0, tdData.length -1); + tdData += "),"; + }); + tdData = $.trim(tdData).substring(0, tdData.length -1); + tdData += ";"; + + //output + //console.log(tdData); + + if(defaults.consoleLog == 'true'){ + console.log(tdData); + } + + var base64data = "base64," + $.base64.encode(tdData); + //window.open('data:text/plain;filename=filename.sql;' + base64data); + download('data:text/plain;'+base64data,'download.'+defaults.type,'text/plain'); + + }else if(defaults.type == 'json'){ + + var jsonHeaderArray = []; + $(el).find('thead').find('tr').each(function() { + var tdData =""; + var jsonArrayTd = []; + + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + jsonArrayTd.push(parseString($(this))); + } + } + }); + jsonHeaderArray.push(jsonArrayTd); + + }); + + var jsonArray = []; + $(el).find('tbody').find('tr').each(function() { + var tdData =""; + var jsonArrayTd = []; + + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + jsonArrayTd.push(parseString($(this))); + } + } + }); + jsonArray.push(jsonArrayTd); + + }); + + var jsonExportArray =[]; + jsonExportArray.push({header:jsonHeaderArray,data:jsonArray}); + + //Return as JSON + //console.log(JSON.stringify(jsonExportArray)); + + //Return as Array + //console.log(jsonExportArray); + if(defaults.consoleLog == 'true'){ + console.log(JSON.stringify(jsonExportArray)); + } + var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray)); + //window.open('data:application/json;filename=exportData;' + base64data); + download('data:application/json;'+base64data,'download.'+defaults.type,'application/json'); + }else if(defaults.type == 'xml'){ + + var xml = ''; + xml += ''; + + // Header + $(el).find('thead').find('tr').each(function() { + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + xml += "" + parseString($(this)) + ""; + } + } + }); + }); + xml += ''; + + // Row Vs Column + var rowCount=1; + $(el).find('tbody').find('tr').each(function() { + xml += ''; + var colCount=0; + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + xml += ""+parseString($(this))+""; + } + } + colCount++; + }); + rowCount++; + xml += ''; + }); + xml += '' + + if(defaults.consoleLog == 'true'){ + console.log(xml); + } + + var base64data = "base64," + $.base64.encode(xml); + //window.open('data:application/xml;filename=exportData;' + base64data); + download('data:application/xml;'+base64data,'download.'+defaults.type,'application/xml'); + + }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){ + + //console.log($(this).html()); + var excel=""; + // Header + $(el).find('thead').find('tr').each(function() { + excel += ""; + $(this).filter(':visible').find('th').each(function(index,data) { + var bIsStateColumn = $(this).attr('data-field') === 'state'; + var bIsActionColumn = $(this).attr('data-field') === 'action'; + if (bIsStateColumn || bIsActionColumn) { + defaults.ignoreColumn.push(index); + } + if ($(this).css('display') != 'none' && !bIsActionColumn && !bIsStateColumn){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + excel += ""; + } + } + }); + excel += ''; + + }); + + // Row Vs Column + var rowCount=1; + $(el).find('tbody').find('tr').each(function() { + excel += ""; + var colCount=0; + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + excel += ""; + } + } + colCount++; + }); + rowCount++; + excel += ''; + }); + excel += '
" + parseString($(this))+ "
"+parseString($(this))+"
' + + if(defaults.consoleLog == 'true'){ + console.log(excel); + } + + var excelFile = ""; + excelFile += ""; + excelFile += ""; + excelFile += ""; + excelFile += ""; + excelFile += excel; + excelFile += ""; + excelFile += ""; + + var base64data = "base64," + $.base64.encode(excelFile); + switch(defaults.type){ + case 'excel': + //window.open('data:application/vnd.ms-'+defaults.type+';'+base64data); + download('data:text/plain;'+base64data,'download.xls','application/vnd.ms-'+defaults.type); + break; + case 'powerpoint': + window.open('data:application/vnd.ms-'+defaults.type+';'+base64data); + break; + case 'doc': + download('data:application/msword;'+base64data,'download.doc','application/msword'); + break; + } + + }else if(defaults.type == 'png'){ + html2canvas($(el), { + onrendered: function(canvas) { + var img = canvas.toDataURL("image/png"); + window.open(img); + } + }); + }else if(defaults.type == 'pdf'){ + + var doc = new jsPDF('p','pt', 'a4', true); + doc.setFontSize(defaults.pdfFontSize); + + // Header + var startColPosition=defaults.pdfLeftMargin; + $(el).find('thead').find('tr').each(function() { + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + var colPosition = startColPosition+ (index * 50); + doc.text(colPosition,20, parseString($(this))); + } + } + }); + }); + + // Row Vs Column + var startRowPosition = 20; var page =1;var rowPosition=0; + $(el).find('tbody').find('tr').each(function(index,data) { + rowCalc = index+1; + + if (rowCalc % 26 == 0){ + doc.addPage(); + page++; + startRowPosition=startRowPosition+10; + } + rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280); + + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + var colPosition = startColPosition+ (index * 50); + doc.text(colPosition,rowPosition, parseString($(this))); + } + } + + }); + + }); + + // Output as Data URI + doc.output('datauri'); + + } + + function parseString(data){ + if(defaults.htmlContent == 'true'){ + content_data = data.html().trim(); + }else{ + if ($(data).find('[data-field-title]').length > 0){ + content_data = $(data).find('[data-field-title]').attr('data-field-title'); + } else if ($(data).find('span[class=button-text]').length > 0){ + content_data = $(data).find('span[class=button-text]').text().trim(); + } else { + content_data = data.text().trim(); + } + } + + if(defaults.escape == 'true'){ + content_data = escape(content_data); + } + + return content_data; + } + + } + }); +})(jQuery); \ No newline at end of file