diff --git a/tableExport.js b/tableExport.js index 1bfaa0fc..3c008f32 100644 --- a/tableExport.js +++ b/tableExport.js @@ -21,41 +21,65 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ (function($){ + + //taken from http://stackoverflow.com/questions/283956/is-there-any-way-to-specify-a-suggested-filename-when-using-data-uri#15832569 + function downloadWithName(uri, name) { + function eventFire(el, etype){ + if (el.fireEvent) { + (el.fireEvent('on' + etype)); + } else { + var evObj = document.createEvent('Events'); + evObj.initEvent(etype, true, false); + el.dispatchEvent(evObj); + } + } + + var link = document.createElement("a"); + link.download = name; + link.href = uri; + eventFire(link, "click"); + } + $.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; - + var available_types = ["csv", "txt", "sql", "json", "xml", "excel", "doc", "powerpoint", "png", "pdf"]; + + var mime_type = ''; + var base64data = ''; + var file_name = ''; + if(defaults.type == 'csv' || defaults.type == 'txt'){ - + // Header var tdData =""; $(el).find('thead').find('tr').each(function() { - tdData += "\n"; + 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 += '"' + 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"; @@ -69,26 +93,28 @@ THE SOFTWARE.*/ //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); + + base64data = "base64," + $.base64.encode(tdData); + mime_type = "data:application/"+defaults.type; + file_name = "exported_data."+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 += '`' + parseString($(this)) + '`,' ; } } - + }); tdData = $.trim(tdData); tdData = $.trim(tdData).substring(0, tdData.length -1); @@ -104,112 +130,115 @@ THE SOFTWARE.*/ } } }); - + 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); - - + + base64data = "base64," + $.base64.encode(tdData); + mime_type = "data:application/"+defaults.type; + file_name = "exported_data."+defaults.type; + }else if(defaults.type == 'json'){ - + var jsonHeaderArray = []; $(el).find('thead').find('tr').each(function() { - var tdData =""; + 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))); + jsonArrayTd.push(parseString($(this))); } } - }); - jsonHeaderArray.push(jsonArrayTd); - + }); + jsonHeaderArray.push(jsonArrayTd); + }); - + var jsonArray = []; $(el).find('tbody').find('tr').each(function() { - var tdData =""; + 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))); + jsonArrayTd.push(parseString($(this))); } } - }); - jsonArray.push(jsonArrayTd); - + }); + 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); + + base64data = "base64," + $.base64.encode(jsonExportArray); + mime_type = "data:application/"+defaults.type; + file_name = "exported_data."+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 ($(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 ($(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); + + base64data = "base64," + $.base64.encode(xml); + mime_type = "data:application/"+defaults.type; + file_name = "exported_data."+defaults.type; }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){ //console.log($(this).html()); @@ -218,39 +247,39 @@ THE SOFTWARE.*/ $(el).find('thead').find('tr').each(function() { excel += ""; $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ + if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ excel += "" + parseString($(this))+ ""; } } - }); - 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 ($(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 += "