From 3f0ebf7243d8f67d834439bb936bb5c36e55fd4f Mon Sep 17 00:00:00 2001
From: Bernardo Oliveira Pinto
Date: Wed, 19 Nov 2014 09:50:16 +0000
Subject: [PATCH 1/6] Correct file download
Wasn't working on Chrome stable: was ignoring file name and extension.
See https://code.google.com/p/chromium/issues/detail?id=373182
---
tableExport.js | 677 +++++++++++++++++++++++++------------------------
1 file changed, 348 insertions(+), 329 deletions(-)
diff --git a/tableExport.js b/tableExport.js
index 1bfaa0fc..d7cfd70e 100644
--- a/tableExport.js
+++ b/tableExport.js
@@ -1,6 +1,9 @@
/*The MIT License (MIT)
-Copyright (c) 2014 https://github.com/kayalshri/
+Oririnal code (c) 2014 https://github.com/kayalshri/
+
+Copyright (c) 2014 https://github.com/bL0p/
+
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -24,336 +27,352 @@ THE SOFTWARE.*/
$.fn.extend({
tableExport: function(options) {
var defaults = {
- separator: ',',
- ignoreColumn: [],
- tableName:'yourTableName',
- type:'csv',
- pdfFontSize:14,
- pdfLeftMargin:20,
- escape:'true',
- htmlContent:'false',
- consoleLog:'false'
- };
+ separator: ',',
+ ignoreColumn: [],
+ tableName:'yourTableName',
+ type:'csv',
+ pdfFontSize:14,
+ pdfLeftMargin:20,
+ escape:'true',
+ htmlContent:'false',
+ consoleLog:'false'
+ };
- var options = $.extend(defaults, options);
- var el = this;
-
- if(defaults.type == 'csv' || defaults.type == 'txt'){
-
- // Header
- var tdData ="";
- $(el).find('thead').find('tr').each(function() {
- tdData += "\n";
- $(this).filter(':visible').find('th').each(function(index,data) {
- if ($(this).css('display') != 'none'){
- if(defaults.ignoreColumn.indexOf(index) == -1){
- tdData += '"' + parseString($(this)) + '"' + defaults.separator;
- }
- }
-
- });
- tdData = $.trim(tdData);
- tdData = $.trim(tdData).substring(0, tdData.length -1);
- });
-
- // Row vs Column
- $(el).find('tbody').find('tr').each(function() {
- tdData += "\n";
- $(this).filter(':visible').find('td').each(function(index,data) {
- if ($(this).css('display') != 'none'){
- if(defaults.ignoreColumn.indexOf(index) == -1){
- tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
- }
- }
- });
- //tdData = $.trim(tdData);
- tdData = $.trim(tdData).substring(0, tdData.length -1);
- });
-
- //output
- if(defaults.consoleLog == 'true'){
- console.log(tdData);
- }
- var base64data = "base64," + $.base64.encode(tdData);
- window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);
- }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:application/sql;filename=exportData;' + base64data);
-
-
- }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);
- }else if(defaults.type == 'xml'){
-
- var xml = '';
- xml += '';
+ var options = $.extend(defaults, options);
+ var el = this;
+
+ if(defaults.type == 'csv' || defaults.type == 'txt'){
+
+ // Header
+ var tdData ="";
+ $(el).find('thead').find('tr').each(function() {
+ tdData += "\n";
+ $(this).filter(':visible').find('th').each(function(index,data) {
+ if ($(this).css('display') != 'none'){
+ if(defaults.ignoreColumn.indexOf(index) == -1){
+ tdData += '"' + parseString($(this)) + '"' + defaults.separator;
+ }
+ }
+
+ });
+ tdData = $.trim(tdData);
+ tdData = $.trim(tdData).substring(0, tdData.length -1);
+ });
+
+ // Row vs Column
+ $(el).find('tbody').find('tr').each(function() {
+ tdData += "\n";
+ $(this).filter(':visible').find('td').each(function(index,data) {
+ if ($(this).css('display') != 'none'){
+ if(defaults.ignoreColumn.indexOf(index) == -1){
+ tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
+ }
+ }
+ });
+ tdData = $.trim(tdData).substring(0, tdData.length -1);
+ });
+
+ //output
+ if(defaults.consoleLog == 'true'){
+ console.log(tdData);
+ }
+
+ downloadFile(tdData, 'plain/text', 'exportData.' + defaults.type);
+ }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);
+ }
+
+ downloadFile(tdData, 'plain/text', 'exportData.' + defaults.type);
+ }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 jsonContent = JSON.stringify(jsonExportArray);
+ downloadFile(jsonContent, 'plain/text', 'exportData.' + defaults.type);
+ }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);
+ // 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);
+ }
+
+ downloadFile(xml, 'plain/text', 'exportData.' + defaults.type);
- }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 += "| " + parseString($(this))+ " | ";
- }
- }
- });
- 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 += "| "+parseString($(this))+" | ";
- }
- }
- colCount++;
- });
- rowCount++;
- excel += '
';
- });
- excel += '
'
-
- if(defaults.consoleLog == 'true'){
- console.log(excel);
- }
-
- var excelFile = "";
- excelFile += "";
- excelFile += "";
- excelFile += "";
- excelFile += "";
- excelFile += excel;
- excelFile += "";
- excelFile += "";
+ }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 += "| " + parseString($(this))+ " | ";
+ }
+ }
+ });
+ 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 += "| "+parseString($(this))+" | ";
+ }
+ }
+ colCount++;
+ });
+ rowCount++;
+ excel += '
';
+ });
+ excel += '
'
+
+ 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);
+ var fileType, fileExtension;
+
+ if(defaults.type == 'excel') {
+ fileType = 'application/vnd.ms-excel';
+ fileExtension = 'xls';
+ } else if(defaults.type == 'doc') {
+ fileType = 'application/msword';
+ fileExtension = 'doc';
+ } else if(defaults.type == 'powerpoint') {
+ fileType = 'application/vnd.ms-powerpoint';
+ fileExtension = 'ppt';
+ }
+ downloadFile(excelFile, fileType, 'exportData.' + fileExtension);
+
+ }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 downloadFile(data, fileType, filename) {
+ var pom = document.createElement('a');
+
+ var blob = new Blob([data], { type: fileType });
+ var url = URL.createObjectURL(blob);
+ pom.href = url;
+
+ pom.setAttribute('download', filename);
+ pom.click();
+ }
+
+
+ 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);
From 7360aee8de49bc3f88701656717da1d13eed8e3c Mon Sep 17 00:00:00 2001
From: Bernardo Oliveira Pinto
Date: Wed, 19 Nov 2014 09:50:38 +0000
Subject: [PATCH 2/6] Remove tableExport.jquery.json file
---
tableExport.jquery.json | 1 -
1 file changed, 1 deletion(-)
delete mode 100644 tableExport.jquery.json
diff --git a/tableExport.jquery.json b/tableExport.jquery.json
deleted file mode 100644
index 8b137891..00000000
--- a/tableExport.jquery.json
+++ /dev/null
@@ -1 +0,0 @@
-
From 775ee98167fd983ac252c272933e737e23c5a22d Mon Sep 17 00:00:00 2001
From: Bernardo Oliveira Pinto
Date: Wed, 19 Nov 2014 09:51:19 +0000
Subject: [PATCH 3/6] Add test file
Not finished (only with Excel and CSV)
---
test/index.html | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
create mode 100644 test/index.html
diff --git a/test/index.html b/test/index.html
new file mode 100644
index 00000000..fc887988
--- /dev/null
+++ b/test/index.html
@@ -0,0 +1,48 @@
+
+
+Test tableExport
+
+
+
+Table with thead and tbody
+
+
+
+ | Header 1 |
+ Header 2 |
+ Header 3 |
+
+
+
+
+ | Row 1 Col 1 |
+ Row 1 Col 2 |
+ Row 1 Col 3 |
+
+
+ | Row 2 Col 1 |
+ Row 2 Col 2 |
+ Row 3 Col 3 |
+
+
+ | Row 3 Col 1 |
+ Row 3 Col 2 |
+ Row 3 Col 3 |
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
From 6e297355d5426be2a96d2695e67456a61f665c6c Mon Sep 17 00:00:00 2001
From: Bernardo Oliveira Pinto
Date: Wed, 19 Nov 2014 15:13:53 +0000
Subject: [PATCH 4/6] Add buttons to export to more formats
---
test/index.html | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/test/index.html b/test/index.html
index fc887988..ee1f18a6 100644
--- a/test/index.html
+++ b/test/index.html
@@ -36,9 +36,15 @@ Table with thead and tbody
-
+
+
+
+
+
+
+
From 8514d38e0122255d5fcb3c8088130b2000962e92 Mon Sep 17 00:00:00 2001
From: Bernardo Oliveira Pinto
Date: Wed, 19 Nov 2014 15:14:25 +0000
Subject: [PATCH 5/6] Remove PowerPoint export (since it doesn't work
currently)
---
tableExport.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tableExport.js b/tableExport.js
index d7cfd70e..0f4ed83f 100644
--- a/tableExport.js
+++ b/tableExport.js
@@ -210,7 +210,7 @@ THE SOFTWARE.*/
downloadFile(xml, 'plain/text', 'exportData.' + defaults.type);
- }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){
+ }else if(defaults.type == 'excel' || defaults.type == 'doc'){
//console.log($(this).html());
var excel="";
// Header
From 32400df3dce5f6207239d5e1e31258968b5e19fa Mon Sep 17 00:00:00 2001
From: Bernardo Oliveira Pinto
Date: Wed, 19 Nov 2014 15:15:21 +0000
Subject: [PATCH 6/6] Remove PowerPoint export (since it doesn't work
currently)
---
tableExport.js | 3 ---
1 file changed, 3 deletions(-)
diff --git a/tableExport.js b/tableExport.js
index 0f4ed83f..d2b3c782 100644
--- a/tableExport.js
+++ b/tableExport.js
@@ -282,9 +282,6 @@ THE SOFTWARE.*/
} else if(defaults.type == 'doc') {
fileType = 'application/msword';
fileExtension = 'doc';
- } else if(defaults.type == 'powerpoint') {
- fileType = 'application/vnd.ms-powerpoint';
- fileExtension = 'ppt';
}
downloadFile(excelFile, fileType, 'exportData.' + fileExtension);