jquery - Export table to CSV in JavaScript not working for table input elements -
i have script exporting output table csv in javascript. various reasons relating (someone else's) ajax code, trigger link instead of submit button.
html:
<a href="#" class="export"><img alt="exporttoexcel" src="../export1.png"></a>
javascript:
(".export").on('click', function (event) { var d = new date(); exporttabletocsv.apply(this, [$('#table1'), 'export' + d.gettime() + '.csv']); }); function exporttabletocsv($table, filename) { csv=createcsv($table,"yes"); csvdata = 'data:application/csv;charset=utf-8,' + encodeuricomponent(csv); if (navigator.mssaveoropenblob) { // separate deal ms browsers var blob = new blob([csv],{type: "text/csv;charset=utf-8;"}); navigator.mssaveoropenblob(blob, filename + ".csv") } else{ $(this) .attr({ 'download': filename, 'href': csvdata, 'target': '_blank' }); } } function createcsv($table,$vis){ if($vis==="yes"){ var $rows = $table.find('tr:has(td):visible'); }else {var $rows = $table.find('tr:has(td)');} tmpcoldelim = string.fromcharcode(11), tmprowdelim = string.fromcharcode(0), coldelim = '","', rowdelim = '"\r\n"', csv1 = '"' + $rows.map(function (i, row) { var $row = $(row); if($vis==="yes"){$cols = $row.find('td:visible'); }else {$cols = $row.find('td');} return $cols.map(function (j, col) { var $col = $(col), text = $col.text(); return text.replace(/"/g, '""'); }).get().join(tmpcoldelim); }).get().join(tmprowdelim) .split(tmprowdelim).join(rowdelim) .split(tmpcoldelim).join(coldelim) + '"'; return csv1; } });
this working fine, added text input fields rows in table. export leaves blank if data inputted. assume because i'm not resubmitting form, can data fields ('.tr_classname').serialize()
.
in line
text = $col.text();
you getting text, if want content of text input, should use
text = $col.val();
knowing this, have evaluate if content of td input text or text
Comments
Post a Comment