diff --git a/tableExport.js b/tableExport.js index 1bfaa0fc..eb266790 100644 --- a/tableExport.js +++ b/tableExport.js @@ -1,25 +1,3 @@ -/*The MIT License (MIT) - -Copyright (c) 2014 https://github.com/kayalshri/ - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE.*/ - (function($){ $.fn.extend({ tableExport: function(options) { @@ -32,7 +10,7 @@ THE SOFTWARE.*/ pdfLeftMargin:20, escape:'true', htmlContent:'false', - consoleLog:'false' + consoleLog:'true' }; var options = $.extend(defaults, options); @@ -217,15 +195,15 @@ THE SOFTWARE.*/ // Header $(el).find('thead').find('tr').each(function() { excel += ""; - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ + $(this).filter(':visible').find('th, td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + var color = $(this).css('background-color'); if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += "" + parseString($(this))+ ""; + excel += parseItemData($(this),'th',color); } } }); excel += ''; - }); @@ -234,14 +212,18 @@ THE SOFTWARE.*/ $(el).find('tbody').find('tr').each(function() { excel += ""; var colCount=0; - $(this).filter(':visible').find('td').each(function(index,data) { + $(this).filter(':visible').find('td, th').each(function(index,data) { if ($(this).css('display') != 'none'){ + //Gets css attributes + var color = $(this).css('background-color'); if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += ""+parseString($(this))+""; + excel += parseItemData($(this),'td',color); } } colCount++; - }); + }); + + rowCount++; excel += ''; }); @@ -274,9 +256,12 @@ THE SOFTWARE.*/ excelFile += excel; excelFile += ""; excelFile += ""; - + + //Export function var base64data = "base64," + $.base64.encode(excelFile); - window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data); + var extension = 'xls'; + openDownloadLink("Export" + '.' + extension, 'data:application/vnd.ms-'+defaults.type+';charset=utf-8;filename='+defaults.filename+'.'+extension+';' + base64data); + }else if(defaults.type == 'png'){ html2canvas($(el), { @@ -352,8 +337,47 @@ THE SOFTWARE.*/ return content_data; } - + + // Analize row attributes and compose new with them + function parseItemData(item, type,color){ + var spanAtt = false; + var fila = item[0]; + var excelTd=""; + if(color == 'transparent'){ + color = 'rgb(255,255,255)'; + } + //Check for attributes + for(var i=0; i<= fila.attributes.length -1 ; i++){ + if(fila.attributes[i].name == "colspan" || fila.attributes[i].name == "rowspan"){ + excelTd += '<' + type + ' '+fila.attributes[i].name + ' = ' + fila.attributes[i].nodeValue + ' bgcolor = "'+ rgb2hex(color) +'">' + parseString($(item))+ ''; + spanAtt = true; + break; + } + } + if(!spanAtt){ + excelTd += '<' + type + ' bgcolor = "' + rgb2hex(color) + '" >' + parseString($(item)) + ''; + } + return excelTd; + } + + function rgb2hex(rgb){ + rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i); + return (rgb && rgb.length === 4) ? "#" + + ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) + + ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) + + ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : ''; + } + + 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); - + \ No newline at end of file