@@ -24,18 +24,20 @@ THE SOFTWARE.*/
24
24
$ . fn . extend ( {
25
25
tableExport : function ( options ) {
26
26
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'
33
35
} ;
34
36
35
37
var options = $ . extend ( defaults , options ) ;
36
38
var el = this ;
37
39
38
- if ( defaults . type == 'csv' ) {
40
+ if ( defaults . type == 'csv' || defaults . type == 'txt' ) {
39
41
40
42
// Header
41
43
var tdData = "" ;
@@ -44,7 +46,7 @@ THE SOFTWARE.*/
44
46
$ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
45
47
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
46
48
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
47
- tdData += '"' + $ ( this ) . text ( ) . trim ( ) + '"' + defaults . separator ;
49
+ tdData += '"' + parseString ( $ ( this ) ) + '"' + defaults . separator ;
48
50
}
49
51
}
50
52
@@ -59,7 +61,7 @@ THE SOFTWARE.*/
59
61
$ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
60
62
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
61
63
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
62
- tdData += '"' + $ ( this ) . text ( ) . trim ( ) + '"' + defaults . separator ;
64
+ tdData += '"' + parseString ( $ ( this ) ) + '"' + defaults . separator ;
63
65
}
64
66
}
65
67
} ) ;
@@ -70,45 +72,7 @@ THE SOFTWARE.*/
70
72
//output
71
73
//console.log(tdData);
72
74
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 ) ;
112
76
} else if ( defaults . type == 'sql' ) {
113
77
114
78
// Header
@@ -118,7 +82,7 @@ THE SOFTWARE.*/
118
82
$ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
119
83
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
120
84
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
121
- tdData += '`' + $ ( this ) . text ( ) . trim ( ) + '`,' ;
85
+ tdData += '`' + parseString ( $ ( this ) ) + '`,' ;
122
86
}
123
87
}
124
88
@@ -133,7 +97,7 @@ THE SOFTWARE.*/
133
97
$ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
134
98
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
135
99
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
136
- tdData += '"' + escape ( $ ( this ) . text ( ) . trim ( ) ) + '",' ;
100
+ tdData += '"' + parseString ( $ ( this ) ) + '",' ;
137
101
}
138
102
}
139
103
} ) ;
@@ -160,7 +124,7 @@ THE SOFTWARE.*/
160
124
$ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
161
125
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
162
126
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
163
- jsonArrayTd . push ( escape ( $ ( this ) . text ( ) . trim ( ) ) ) ;
127
+ jsonArrayTd . push ( parseString ( $ ( this ) ) ) ;
164
128
}
165
129
}
166
130
} ) ;
@@ -176,7 +140,7 @@ THE SOFTWARE.*/
176
140
$ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
177
141
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
178
142
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
179
- jsonArrayTd . push ( escape ( $ ( this ) . text ( ) . trim ( ) ) ) ;
143
+ jsonArrayTd . push ( parseString ( $ ( this ) ) ) ;
180
144
}
181
145
}
182
146
} ) ;
@@ -205,7 +169,7 @@ THE SOFTWARE.*/
205
169
$ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
206
170
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
207
171
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
208
- xml += "<field>" + escape ( $ ( this ) . text ( ) . trim ( ) ) + "</field>" ;
172
+ xml += "<field>" + parseString ( $ ( this ) ) + "</field>" ;
209
173
}
210
174
}
211
175
} ) ;
@@ -220,7 +184,7 @@ THE SOFTWARE.*/
220
184
$ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
221
185
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
222
186
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
223
- xml += "<column-" + colCount + ">" + escape ( $ ( this ) . text ( ) . trim ( ) ) + "</column-" + colCount + ">" ;
187
+ xml += "<column-" + colCount + ">" + parseString ( $ ( this ) ) + "</column-" + colCount + ">" ;
224
188
}
225
189
}
226
190
colCount ++ ;
@@ -243,7 +207,7 @@ THE SOFTWARE.*/
243
207
$ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
244
208
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
245
209
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
246
- excel += "<td>" + $ ( this ) . html ( ) . trim ( ) + "</td>" ;
210
+ excel += "<td>" + parseString ( $ ( this ) ) + "</td>" ;
247
211
}
248
212
}
249
213
} ) ;
@@ -260,7 +224,7 @@ THE SOFTWARE.*/
260
224
$ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
261
225
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
262
226
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
263
- excel += "<td>" + $ ( this ) . html ( ) . trim ( ) + "</td>" ;
227
+ excel += "<td>" + parseString ( $ ( this ) ) + "</td>" ;
264
228
}
265
229
}
266
230
colCount ++ ;
@@ -320,7 +284,7 @@ THE SOFTWARE.*/
320
284
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
321
285
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
322
286
var colPosition = startColPosition + ( index * 50 ) ;
323
- doc . text ( colPosition , 20 , $ ( this ) . text ( ) . trim ( ) ) ;
287
+ doc . text ( colPosition , 20 , parseString ( $ ( this ) ) ) ;
324
288
}
325
289
}
326
290
} ) ;
@@ -343,19 +307,34 @@ THE SOFTWARE.*/
343
307
if ( $ ( this ) . css ( 'display' ) != 'none' ) {
344
308
if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
345
309
var colPosition = startColPosition + ( index * 50 ) ;
346
- doc . text ( colPosition , rowPosition , $ ( this ) . text ( ) . trim ( ) ) ;
310
+ doc . text ( colPosition , rowPosition , parseString ( $ ( this ) ) ) ;
347
311
}
348
312
}
349
313
350
314
} ) ;
351
315
352
316
} ) ;
353
- //doc.text(20,rowPosition+10, "test Msg");
354
-
317
+
355
318
// Output as Data URI
356
319
doc . output ( 'datauri' ) ;
357
320
358
321
}
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
+ }
359
338
360
339
}
361
340
} ) ;
0 commit comments