Skip to content

Commit 388ca14

Browse files
committed
Update tableExport.js
1 parent 11de86a commit 388ca14

File tree

1 file changed

+39
-60
lines changed

1 file changed

+39
-60
lines changed

tableExport.js

Lines changed: 39 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@ THE SOFTWARE.*/
2424
$.fn.extend({
2525
tableExport: function(options) {
2626
var defaults = {
27-
separator: ',',
28-
ignoreColumn: [],
29-
tableName:'yourTableName',
30-
type:'csv',
31-
pdfFontSize:14,
32-
pdfLeftMargin:20
27+
separator: ',',
28+
ignoreColumn: [],
29+
tableName:'yourTableName',
30+
type:'csv',
31+
pdfFontSize:14,
32+
pdfLeftMargin:20,
33+
escape:'true',
34+
htmlContent:'false'
3335
};
3436

3537
var options = $.extend(defaults, options);
3638
var el = this;
3739

38-
if(defaults.type == 'csv'){
40+
if(defaults.type == 'csv' || defaults.type == 'txt'){
3941

4042
// Header
4143
var tdData ="";
@@ -44,7 +46,7 @@ THE SOFTWARE.*/
4446
$(this).filter(':visible').find('th').each(function(index,data) {
4547
if ($(this).css('display') != 'none'){
4648
if(defaults.ignoreColumn.indexOf(index) == -1){
47-
tdData += '"' + $(this).text().trim() + '"' + defaults.separator;
49+
tdData += '"' + parseString($(this)) + '"' + defaults.separator;
4850
}
4951
}
5052

@@ -59,7 +61,7 @@ THE SOFTWARE.*/
5961
$(this).filter(':visible').find('td').each(function(index,data) {
6062
if ($(this).css('display') != 'none'){
6163
if(defaults.ignoreColumn.indexOf(index) == -1){
62-
tdData += '"'+ $(this).text().trim() + '"'+ defaults.separator;
64+
tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
6365
}
6466
}
6567
});
@@ -70,45 +72,7 @@ THE SOFTWARE.*/
7072
//output
7173
//console.log(tdData);
7274
var base64data = "base64," + $.base64.encode(tdData);
73-
window.open('data:application/csv;filename=exportData;' + base64data);
74-
}else if(defaults.type == 'txt'){
75-
76-
// Header
77-
var tdData ="";
78-
$(el).find('thead').find('tr').each(function() {
79-
tdData += "\n";
80-
$(this).filter(':visible').find('th').each(function(index,data) {
81-
if ($(this).css('display') != 'none'){
82-
if(defaults.ignoreColumn.indexOf(index) == -1){
83-
tdData += '"' + $(this).text().trim() + '"' + defaults.separator;
84-
}
85-
}
86-
87-
});
88-
tdData = $.trim(tdData);
89-
tdData = $.trim(tdData).substring(0, tdData.length -1);
90-
});
91-
92-
// Row vs Column
93-
$(el).find('tbody').find('tr').each(function() {
94-
tdData += "\n";
95-
$(this).filter(':visible').find('td').each(function(index,data) {
96-
if ($(this).css('display') != 'none'){
97-
if(defaults.ignoreColumn.indexOf(index) == -1){
98-
tdData += '"'+ $(this).text().trim() + '"'+ defaults.separator;
99-
}
100-
}
101-
});
102-
//tdData = $.trim(tdData);
103-
tdData = $.trim(tdData).substring(0, tdData.length -1);
104-
});
105-
106-
//output
107-
//console.log(tdData);
108-
var base64data = "base64," + $.base64.encode(tdData);
109-
window.open('data:application/txt;filename=exportData;' + base64data);
110-
111-
75+
window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);
11276
}else if(defaults.type == 'sql'){
11377

11478
// Header
@@ -118,7 +82,7 @@ THE SOFTWARE.*/
11882
$(this).filter(':visible').find('th').each(function(index,data) {
11983
if ($(this).css('display') != 'none'){
12084
if(defaults.ignoreColumn.indexOf(index) == -1){
121-
tdData += '`' + $(this).text().trim() + '`,' ;
85+
tdData += '`' + parseString($(this)) + '`,' ;
12286
}
12387
}
12488

@@ -133,7 +97,7 @@ THE SOFTWARE.*/
13397
$(this).filter(':visible').find('td').each(function(index,data) {
13498
if ($(this).css('display') != 'none'){
13599
if(defaults.ignoreColumn.indexOf(index) == -1){
136-
tdData += '"'+ escape($(this).text().trim()) + '",';
100+
tdData += '"'+ parseString($(this)) + '",';
137101
}
138102
}
139103
});
@@ -160,7 +124,7 @@ THE SOFTWARE.*/
160124
$(this).filter(':visible').find('th').each(function(index,data) {
161125
if ($(this).css('display') != 'none'){
162126
if(defaults.ignoreColumn.indexOf(index) == -1){
163-
jsonArrayTd.push(escape($(this).text().trim()));
127+
jsonArrayTd.push(parseString($(this)));
164128
}
165129
}
166130
});
@@ -176,7 +140,7 @@ THE SOFTWARE.*/
176140
$(this).filter(':visible').find('td').each(function(index,data) {
177141
if ($(this).css('display') != 'none'){
178142
if(defaults.ignoreColumn.indexOf(index) == -1){
179-
jsonArrayTd.push(escape($(this).text().trim()));
143+
jsonArrayTd.push(parseString($(this)));
180144
}
181145
}
182146
});
@@ -205,7 +169,7 @@ THE SOFTWARE.*/
205169
$(this).filter(':visible').find('th').each(function(index,data) {
206170
if ($(this).css('display') != 'none'){
207171
if(defaults.ignoreColumn.indexOf(index) == -1){
208-
xml += "<field>" + escape($(this).text().trim()) + "</field>";
172+
xml += "<field>" + parseString($(this)) + "</field>";
209173
}
210174
}
211175
});
@@ -220,7 +184,7 @@ THE SOFTWARE.*/
220184
$(this).filter(':visible').find('td').each(function(index,data) {
221185
if ($(this).css('display') != 'none'){
222186
if(defaults.ignoreColumn.indexOf(index) == -1){
223-
xml += "<column-"+colCount+">"+escape($(this).text().trim())+"</column-"+colCount+">";
187+
xml += "<column-"+colCount+">"+parseString($(this))+"</column-"+colCount+">";
224188
}
225189
}
226190
colCount++;
@@ -243,7 +207,7 @@ THE SOFTWARE.*/
243207
$(this).filter(':visible').find('th').each(function(index,data) {
244208
if ($(this).css('display') != 'none'){
245209
if(defaults.ignoreColumn.indexOf(index) == -1){
246-
excel += "<td>" + $(this).html().trim()+ "</td>";
210+
excel += "<td>" + parseString($(this))+ "</td>";
247211
}
248212
}
249213
});
@@ -260,7 +224,7 @@ THE SOFTWARE.*/
260224
$(this).filter(':visible').find('td').each(function(index,data) {
261225
if ($(this).css('display') != 'none'){
262226
if(defaults.ignoreColumn.indexOf(index) == -1){
263-
excel += "<td>"+$(this).html().trim()+"</td>";
227+
excel += "<td>"+parseString($(this))+"</td>";
264228
}
265229
}
266230
colCount++;
@@ -320,7 +284,7 @@ THE SOFTWARE.*/
320284
if ($(this).css('display') != 'none'){
321285
if(defaults.ignoreColumn.indexOf(index) == -1){
322286
var colPosition = startColPosition+ (index * 50);
323-
doc.text(colPosition,20, $(this).text().trim());
287+
doc.text(colPosition,20, parseString($(this)));
324288
}
325289
}
326290
});
@@ -343,19 +307,34 @@ THE SOFTWARE.*/
343307
if ($(this).css('display') != 'none'){
344308
if(defaults.ignoreColumn.indexOf(index) == -1){
345309
var colPosition = startColPosition+ (index * 50);
346-
doc.text(colPosition,rowPosition, $(this).text().trim());
310+
doc.text(colPosition,rowPosition, parseString($(this)));
347311
}
348312
}
349313

350314
});
351315

352316
});
353-
//doc.text(20,rowPosition+10, "test Msg");
354-
317+
355318
// Output as Data URI
356319
doc.output('datauri');
357320

358321
}
322+
323+
324+
function parseString(data){
325+
326+
if(defaults.htmlContent == 'true'){
327+
content_data = data.html().trim();
328+
}else{
329+
content_data = data.text().trim();
330+
}
331+
332+
if(defaults.escape == 'true'){
333+
content_data = escape(content_data);
334+
}
335+
336+
return content_data;
337+
}
359338

360339
}
361340
});

0 commit comments

Comments
 (0)