Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved UTF-8 support, refactoring (openDownloadLink), enforced downloa... #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions tableExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ THE SOFTWARE.*/
separator: ',',
ignoreColumn: [],
tableName:'yourTableName',
filename:'exportData',
type:'csv',
pdfFontSize:14,
pdfLeftMargin:20,
Expand All @@ -47,7 +48,7 @@ THE SOFTWARE.*/
$(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 += '"' + parseString($(this).find(':visible')) + '"' + defaults.separator;
}
}

Expand All @@ -74,8 +75,15 @@ THE SOFTWARE.*/
if(defaults.consoleLog == 'true'){
console.log(tdData);
}
var base64data = "base64," + $.base64.encode(tdData);
window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);

var mimeType = 'csv';
if(defaults.type === 'txt') {
mimeType = 'plain';
}

var base64data = "base64," + B64.encode(tdData);

openDownloadLink(defaults.filename + '.' + defaults.type, 'data:text/'+mimeType+';charset=utf-8;filename=' + defaults.filename + '.' + defaults.type + ';' + base64data);
}else if(defaults.type == 'sql'){

// Header
Expand All @@ -85,7 +93,7 @@ THE SOFTWARE.*/
$(this).filter(':visible').find('th').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '`' + parseString($(this)) + '`,' ;
tdData += '`' + parseString($(this).find(':visible')) + '`,' ;
}
}

Expand Down Expand Up @@ -118,10 +126,9 @@ THE SOFTWARE.*/
console.log(tdData);
}

var base64data = "base64," + $.base64.encode(tdData);
window.open('data:application/sql;filename=exportData;' + base64data);


var base64data = "base64," + B64.encode(tdData);

openDownloadLink(defaults.filename + '.sql', 'data:application/sql;filename=' + defaults.filename + '.sql;' + base64data);
}else if(defaults.type == 'json'){

var jsonHeaderArray = [];
Expand All @@ -132,7 +139,7 @@ THE SOFTWARE.*/
$(this).filter(':visible').find('th').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
jsonArrayTd.push(parseString($(this)));
jsonArrayTd.push(parseString($(this).find(':visible')));
}
}
});
Expand Down Expand Up @@ -167,8 +174,8 @@ THE SOFTWARE.*/
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);
var base64data = "base64," + B64.encode(JSON.stringify(jsonExportArray));
openDownloadLink(defaults.filename + '.json', 'data:application/json;filename=' + defaults.filename + '.json;' + base64data);
}else if(defaults.type == 'xml'){

var xml = '<?xml version="1.0" encoding="utf-8"?>';
Expand All @@ -179,7 +186,7 @@ THE SOFTWARE.*/
$(this).filter(':visible').find('th').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
xml += "<field>" + parseString($(this)) + "</field>";
xml += "<field>" + parseString($(this).find(':visible')) + "</field>";
}
}
});
Expand Down Expand Up @@ -208,9 +215,8 @@ THE SOFTWARE.*/
console.log(xml);
}

var base64data = "base64," + $.base64.encode(xml);
window.open('data:application/xml;filename=exportData;' + base64data);

var base64data = "base64," + B64.encode(xml);
openDownloadLink(defaults.filename + '.xml', 'data:application/xml;filename=' + defaults.filename + '.xml;' + base64data);
}else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){
//console.log($(this).html());
var excel="<table>";
Expand All @@ -220,7 +226,7 @@ THE SOFTWARE.*/
$(this).filter(':visible').find('th').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
excel += "<td>" + parseString($(this))+ "</td>";
excel += "<td>" + parseString($(this).find(':visible'))+ "</td>";
}
}
});
Expand Down Expand Up @@ -253,6 +259,7 @@ THE SOFTWARE.*/

var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+defaults.type+"' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += "<head>";
excelFile += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
Expand All @@ -275,9 +282,17 @@ THE SOFTWARE.*/
excelFile += "</body>";
excelFile += "</html>";

var base64data = "base64," + $.base64.encode(excelFile);
window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data);

var extension = 'doc';
if(defaults.type === 'excel') {
extension = 'xls';
}
if(defaults.type === 'powerpoint') {
extension = 'ppt';
}

var base64data = "base64," + B64.encode(excelFile);

openDownloadLink(defaults.filename + '.' + extension, 'data:application/vnd.ms-'+defaults.type+';charset=utf-8;filename='+defaults.filename+'.'+extension+';' + base64data);
}else if(defaults.type == 'png'){
html2canvas($(el), {
onrendered: function(canvas) {
Expand All @@ -299,7 +314,7 @@ THE SOFTWARE.*/
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
var colPosition = startColPosition+ (index * 50);
doc.text(colPosition,20, parseString($(this)));
doc.text(colPosition,20, parseString($(this).find(':visible')));
}
}
});
Expand All @@ -311,12 +326,12 @@ THE SOFTWARE.*/
$(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);
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'){
Expand Down Expand Up @@ -352,8 +367,16 @@ THE SOFTWARE.*/

return content_data;
}


function openDownloadLink(filename, href) {
var a = document.createElement('a');
document.body.appendChild(a);
a.style = 'display: none';
a.href = href;
a.download = filename;
a.click();
document.body.removeChild(a);
}
}
});
})(jQuery);