1+ /*The MIT License (MIT)
2+ Copyright (c) 2014 https://github.com/kayalshri/
3+ Permission is hereby granted, free of charge, to any person obtaining a copy
4+ of this software and associated documentation files (the "Software"), to deal
5+ in the Software without restriction, including without limitation the rights
6+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+ copies of the Software, and to permit persons to whom the Software is
8+ furnished to do so, subject to the following conditions:
9+ The above copyright notice and this permission notice shall be included in
10+ all copies or substantial portions of the Software.
11+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
17+ THE SOFTWARE.*/
18+
19+ ( function ( $ ) {
20+ $ . fn . extend ( {
21+ tableExport : function ( options ) {
22+ var defaults = {
23+ separator : ',' ,
24+ ignoreColumn : [ ] ,
25+ tableName :'yourTableName' ,
26+ type :'csv' ,
27+ pdfFontSize :14 ,
28+ pdfLeftMargin :20 ,
29+ escape :'true' ,
30+ htmlContent :'false' ,
31+ consoleLog :'false'
32+ } ;
33+
34+ var options = $ . extend ( defaults , options ) ;
35+ var el = this ;
36+
37+ if ( defaults . type == 'csv' || defaults . type == 'txt' ) {
38+
39+ // Header
40+ var tdData = "" ;
41+ $ ( el ) . find ( 'thead' ) . find ( 'tr' ) . each ( function ( ) {
42+ tdData += "\n" ;
43+ $ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
44+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
45+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
46+ tdData += '"' + parseString ( $ ( this ) ) + '"' + defaults . separator ;
47+ }
48+ }
49+
50+ } ) ;
51+ tdData = $ . trim ( tdData ) ;
52+ tdData = $ . trim ( tdData ) . substring ( 0 , tdData . length - 1 ) ;
53+ } ) ;
54+
55+ // Row vs Column
56+ $ ( el ) . find ( 'tbody' ) . find ( 'tr' ) . each ( function ( ) {
57+ tdData += "\n" ;
58+ $ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
59+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
60+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
61+ tdData += '"' + parseString ( $ ( this ) ) + '"' + defaults . separator ;
62+ }
63+ }
64+ } ) ;
65+ //tdData = $.trim(tdData);
66+ tdData = $ . trim ( tdData ) . substring ( 0 , tdData . length - 1 ) ;
67+ } ) ;
68+
69+ //output
70+ if ( defaults . consoleLog == 'true' ) {
71+ console . log ( tdData ) ;
72+ }
73+ var base64data = "base64," + $ . base64 . encode ( tdData ) ;
74+ window . open ( 'data:application/' + defaults . type + ';filename=exportData;' + base64data ) ;
75+ } else if ( defaults . type == 'sql' ) {
76+
77+ // Header
78+ var tdData = "INSERT INTO `" + defaults . tableName + "` (" ;
79+ $ ( el ) . find ( 'thead' ) . find ( 'tr' ) . each ( function ( ) {
80+
81+ $ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
82+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
83+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
84+ tdData += '`' + parseString ( $ ( this ) ) + '`,' ;
85+ }
86+ }
87+
88+ } ) ;
89+ tdData = $ . trim ( tdData ) ;
90+ tdData = $ . trim ( tdData ) . substring ( 0 , tdData . length - 1 ) ;
91+ } ) ;
92+ tdData += ") VALUES " ;
93+ // Row vs Column
94+ $ ( el ) . find ( 'tbody' ) . find ( 'tr' ) . each ( function ( ) {
95+ tdData += "(" ;
96+ $ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
97+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
98+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
99+ tdData += '"' + parseString ( $ ( this ) ) + '",' ;
100+ }
101+ }
102+ } ) ;
103+
104+ tdData = $ . trim ( tdData ) . substring ( 0 , tdData . length - 1 ) ;
105+ tdData += ")," ;
106+ } ) ;
107+ tdData = $ . trim ( tdData ) . substring ( 0 , tdData . length - 1 ) ;
108+ tdData += ";" ;
109+
110+ //output
111+ //console.log(tdData);
112+
113+ if ( defaults . consoleLog == 'true' ) {
114+ console . log ( tdData ) ;
115+ }
116+
117+ var base64data = "base64," + $ . base64 . encode ( tdData ) ;
118+ window . open ( 'data:application/sql;filename=exportData;' + base64data ) ;
119+
120+
121+ } else if ( defaults . type == 'json' ) {
122+
123+ var jsonHeaderArray = [ ] ;
124+ $ ( el ) . find ( 'thead' ) . find ( 'tr' ) . each ( function ( ) {
125+ var tdData = "" ;
126+ var jsonArrayTd = [ ] ;
127+
128+ $ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
129+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
130+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
131+ jsonArrayTd . push ( parseString ( $ ( this ) ) ) ;
132+ }
133+ }
134+ } ) ;
135+ jsonHeaderArray . push ( jsonArrayTd ) ;
136+
137+ } ) ;
138+
139+ var jsonArray = [ ] ;
140+ $ ( el ) . find ( 'tbody' ) . find ( 'tr' ) . each ( function ( ) {
141+ var tdData = "" ;
142+ var jsonArrayTd = [ ] ;
143+
144+ $ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
145+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
146+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
147+ jsonArrayTd . push ( parseString ( $ ( this ) ) ) ;
148+ }
149+ }
150+ } ) ;
151+ jsonArray . push ( jsonArrayTd ) ;
152+
153+ } ) ;
154+
155+ var jsonExportArray = [ ] ;
156+ jsonExportArray . push ( { header :jsonHeaderArray , data :jsonArray } ) ;
157+
158+ //Return as JSON
159+ //console.log(JSON.stringify(jsonExportArray));
160+
161+ //Return as Array
162+ //console.log(jsonExportArray);
163+ if ( defaults . consoleLog == 'true' ) {
164+ console . log ( JSON . stringify ( jsonExportArray ) ) ;
165+ }
166+ var base64data = "base64," + $ . base64 . encode ( JSON . stringify ( jsonExportArray ) ) ;
167+ window . open ( 'data:application/json;filename=exportData;' + base64data ) ;
168+ } else if ( defaults . type == 'xml' ) {
169+
170+ var xml = '<?xml version="1.0" encoding="utf-8"?>' ;
171+ xml += '<tabledata><fields>' ;
172+
173+ // Header
174+ $ ( el ) . find ( 'thead' ) . find ( 'tr' ) . each ( function ( ) {
175+ $ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
176+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
177+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
178+ xml += "<field>" + parseString ( $ ( this ) ) + "</field>" ;
179+ }
180+ }
181+ } ) ;
182+ } ) ;
183+ xml += '</fields><data>' ;
184+
185+ // Row Vs Column
186+ var rowCount = 1 ;
187+ $ ( el ) . find ( 'tbody' ) . find ( 'tr' ) . each ( function ( ) {
188+ xml += '<row id="' + rowCount + '">' ;
189+ var colCount = 0 ;
190+ $ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
191+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
192+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
193+ xml += "<column-" + colCount + ">" + parseString ( $ ( this ) ) + "</column-" + colCount + ">" ;
194+ }
195+ }
196+ colCount ++ ;
197+ } ) ;
198+ rowCount ++ ;
199+ xml += '</row>' ;
200+ } ) ;
201+ xml += '</data></tabledata>'
202+
203+ if ( defaults . consoleLog == 'true' ) {
204+ console . log ( xml ) ;
205+ }
206+
207+ var base64data = "base64," + $ . base64 . encode ( xml ) ;
208+ window . open ( 'data:application/xml;filename=exportData;' + base64data ) ;
209+
210+ } else if ( defaults . type == 'excel' || defaults . type == 'doc' || defaults . type == 'powerpoint' ) {
211+ //console.log($(this).html());
212+ var excel = "<table>" ;
213+ // Header
214+ $ ( el ) . find ( 'thead' ) . find ( 'tr' ) . each ( function ( ) {
215+ excel += "<tr>" ;
216+ $ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
217+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
218+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
219+ excel += "<td>" + parseString ( $ ( this ) ) + "</td>" ;
220+ }
221+ }
222+ } ) ;
223+ excel += '</tr>' ;
224+
225+ } ) ;
226+
227+
228+ // Row Vs Column
229+ var rowCount = 1 ;
230+ $ ( el ) . find ( 'tbody' ) . find ( 'tr' ) . each ( function ( ) {
231+ excel += "<tr>" ;
232+ var colCount = 0 ;
233+ $ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
234+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
235+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
236+ excel += "<td>" + parseString ( $ ( this ) ) + "</td>" ;
237+ }
238+ }
239+ colCount ++ ;
240+ } ) ;
241+ rowCount ++ ;
242+ excel += '</tr>' ;
243+ } ) ;
244+ excel += '</table>'
245+
246+ if ( defaults . consoleLog == 'true' ) {
247+ console . log ( excel ) ;
248+ }
249+
250+ var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:" + defaults . type + "' xmlns='http://www.w3.org/TR/REC-html40'>" ;
251+ excelFile += "<head>" ;
252+ excelFile += "<!--[if gte mso 9]>" ;
253+ excelFile += "<xml>" ;
254+ excelFile += "<x:ExcelWorkbook>" ;
255+ excelFile += "<x:ExcelWorksheets>" ;
256+ excelFile += "<x:ExcelWorksheet>" ;
257+ excelFile += "<x:Name>" ;
258+ excelFile += "{worksheet}" ;
259+ excelFile += "</x:Name>" ;
260+ excelFile += "<x:WorksheetOptions>" ;
261+ excelFile += "<x:DisplayGridlines/>" ;
262+ excelFile += "</x:WorksheetOptions>" ;
263+ excelFile += "</x:ExcelWorksheet>" ;
264+ excelFile += "</x:ExcelWorksheets>" ;
265+ excelFile += "</x:ExcelWorkbook>" ;
266+ excelFile += "</xml>" ;
267+ excelFile += "<![endif]-->" ;
268+ excelFile += "</head>" ;
269+ excelFile += "<body>" ;
270+ excelFile += excel ;
271+ excelFile += "</body>" ;
272+ excelFile += "</html>" ;
273+
274+ var base64data = "base64," + $ . base64 . encode ( excelFile ) ;
275+ window . open ( 'data:application/vnd.ms-' + defaults . type + ';filename=exportData.doc;' + base64data ) ;
276+
277+ } else if ( defaults . type == 'png' ) {
278+ html2canvas ( $ ( el ) , {
279+ onrendered : function ( canvas ) {
280+ var img = canvas . toDataURL ( "image/png" ) ;
281+ window . open ( img ) ;
282+
283+
284+ }
285+ } ) ;
286+ } else if ( defaults . type == 'pdf' ) {
287+
288+ var doc = new jsPDF ( 'p' , 'pt' , 'a4' , true ) ;
289+ doc . setFontSize ( defaults . pdfFontSize ) ;
290+
291+ // Header
292+ var startColPosition = defaults . pdfLeftMargin ;
293+ $ ( el ) . find ( 'thead' ) . find ( 'tr' ) . each ( function ( ) {
294+ $ ( this ) . filter ( ':visible' ) . find ( 'th' ) . each ( function ( index , data ) {
295+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
296+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
297+ var colPosition = startColPosition + ( index * 50 ) ;
298+ doc . text ( colPosition , 20 , parseString ( $ ( this ) ) ) ;
299+ }
300+ }
301+ } ) ;
302+ } ) ;
303+
304+
305+ // Row Vs Column
306+ var startRowPosition = 20 ; var page = 1 ; var rowPosition = 0 ;
307+ $ ( el ) . find ( 'tbody' ) . find ( 'tr' ) . each ( function ( index , data ) {
308+ rowCalc = index + 1 ;
309+
310+ if ( rowCalc % 26 == 0 ) {
311+ doc . addPage ( ) ;
312+ page ++ ;
313+ startRowPosition = startRowPosition + 10 ;
314+ }
315+ rowPosition = ( startRowPosition + ( rowCalc * 10 ) ) - ( ( page - 1 ) * 280 ) ;
316+
317+ $ ( this ) . filter ( ':visible' ) . find ( 'td' ) . each ( function ( index , data ) {
318+ if ( $ ( this ) . css ( 'display' ) != 'none' ) {
319+ if ( defaults . ignoreColumn . indexOf ( index ) == - 1 ) {
320+ var colPosition = startColPosition + ( index * 50 ) ;
321+ doc . text ( colPosition , rowPosition , parseString ( $ ( this ) ) ) ;
322+ }
323+ }
324+
325+ } ) ;
326+
327+ } ) ;
328+
329+ // Output as Data URI
330+ doc . output ( 'datauri' ) ;
331+
332+ }
333+
334+
335+ function parseString ( data ) {
336+
337+ if ( defaults . htmlContent == 'true' ) {
338+ content_data = data . html ( ) . trim ( ) ;
339+ } else {
340+ content_data = data . text ( ) . trim ( ) ;
341+ }
342+
343+ if ( defaults . escape == 'true' ) {
344+ content_data = escape ( content_data ) ;
345+ }
346+
347+
348+
349+ return content_data ;
350+ }
351+
352+ }
353+ } ) ;
354+ } ) ( jQuery ) ;
0 commit comments