1 /*The MIT License (MIT)
3 Copyright (c) 2014 https://github.com/kayalshri/
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 tableExport: function(options) {
29 tableName:'yourTableName',
38 var options = $.extend(defaults, options);
41 if(defaults.type == 'csv' || defaults.type == 'txt'){
45 $(el).find('thead').find('tr').each(function() {
47 $(this).filter(':visible').find('th').each(function(index,data) {
48 if ($(this).css('display') != 'none'){
49 if(defaults.ignoreColumn.indexOf(index) == -1){
50 tdData += '"' + parseString($(this)) + '"' + defaults.separator;
55 tdData = $.trim(tdData);
56 tdData = $.trim(tdData).substring(0, tdData.length -1);
60 $(el).find('tbody').find('tr').each(function() {
62 $(this).filter(':visible').find('td').each(function(index,data) {
63 if ($(this).css('display') != 'none'){
64 if(defaults.ignoreColumn.indexOf(index) == -1){
65 tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
69 //tdData = $.trim(tdData);
70 tdData = $.trim(tdData).substring(0, tdData.length -1);
74 if(defaults.consoleLog == 'true'){
77 var base64data = "base64," + $.base64.encode(tdData);
78 window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);
79 }else if(defaults.type == 'sql'){
82 var tdData ="INSERT INTO `"+defaults.tableName+"` (";
83 $(el).find('thead').find('tr').each(function() {
85 $(this).filter(':visible').find('th').each(function(index,data) {
86 if ($(this).css('display') != 'none'){
87 if(defaults.ignoreColumn.indexOf(index) == -1){
88 tdData += '`' + parseString($(this)) + '`,' ;
93 tdData = $.trim(tdData);
94 tdData = $.trim(tdData).substring(0, tdData.length -1);
96 tdData += ") VALUES ";
98 $(el).find('tbody').find('tr').each(function() {
100 $(this).filter(':visible').find('td').each(function(index,data) {
101 if ($(this).css('display') != 'none'){
102 if(defaults.ignoreColumn.indexOf(index) == -1){
103 tdData += '"'+ parseString($(this)) + '",';
108 tdData = $.trim(tdData).substring(0, tdData.length -1);
111 tdData = $.trim(tdData).substring(0, tdData.length -1);
115 //console.log(tdData);
117 if(defaults.consoleLog == 'true'){
121 var base64data = "base64," + $.base64.encode(tdData);
122 window.open('data:application/sql;filename=exportData;' + base64data);
125 }else if(defaults.type == 'json'){
127 var jsonHeaderArray = [];
128 $(el).find('thead').find('tr').each(function() {
130 var jsonArrayTd = [];
132 $(this).filter(':visible').find('th').each(function(index,data) {
133 if ($(this).css('display') != 'none'){
134 if(defaults.ignoreColumn.indexOf(index) == -1){
135 jsonArrayTd.push(parseString($(this)));
139 jsonHeaderArray.push(jsonArrayTd);
144 $(el).find('tbody').find('tr').each(function() {
146 var jsonArrayTd = [];
148 $(this).filter(':visible').find('td').each(function(index,data) {
149 if ($(this).css('display') != 'none'){
150 if(defaults.ignoreColumn.indexOf(index) == -1){
151 jsonArrayTd.push(parseString($(this)));
155 jsonArray.push(jsonArrayTd);
159 var jsonExportArray =[];
160 jsonExportArray.push({header:jsonHeaderArray,data:jsonArray});
163 //console.log(JSON.stringify(jsonExportArray));
166 //console.log(jsonExportArray);
167 if(defaults.consoleLog == 'true'){
168 console.log(JSON.stringify(jsonExportArray));
170 var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray));
171 window.open('data:application/json;filename=exportData;' + base64data);
172 }else if(defaults.type == 'xml'){
174 var xml = '<?xml version="1.0" encoding="utf-8"?>';
175 xml += '<tabledata><fields>';
178 $(el).find('thead').find('tr').each(function() {
179 $(this).filter(':visible').find('th').each(function(index,data) {
180 if ($(this).css('display') != 'none'){
181 if(defaults.ignoreColumn.indexOf(index) == -1){
182 xml += "<field>" + parseString($(this)) + "</field>";
187 xml += '</fields><data>';
191 $(el).find('tbody').find('tr').each(function() {
192 xml += '<row id="'+rowCount+'">';
194 $(this).filter(':visible').find('td').each(function(index,data) {
195 if ($(this).css('display') != 'none'){
196 if(defaults.ignoreColumn.indexOf(index) == -1){
197 xml += "<column-"+colCount+">"+parseString($(this))+"</column-"+colCount+">";
205 xml += '</data></tabledata>'
207 if(defaults.consoleLog == 'true'){
211 var base64data = "base64," + $.base64.encode(xml);
212 window.open('data:application/xml;filename=exportData;' + base64data);
214 }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){
215 //console.log($(this).html());
218 $(el).find('thead').find('tr').each(function() {
220 $(this).filter(':visible').find('th').each(function(index,data) {
221 if ($(this).css('display') != 'none'){
222 if(defaults.ignoreColumn.indexOf(index) == -1){
223 excel += "<td>" + parseString($(this))+ "</td>";
234 $(el).find('tbody').find('tr').each(function() {
237 $(this).filter(':visible').find('td').each(function(index,data) {
238 if ($(this).css('display') != 'none'){
239 if(defaults.ignoreColumn.indexOf(index) == -1){
240 excel += "<td>"+parseString($(this))+"</td>";
250 if(defaults.consoleLog == 'true'){
254 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'>";
255 excelFile += "<head>";
256 excelFile += "<!--[if gte mso 9]>";
257 excelFile += "<xml>";
258 excelFile += "<x:ExcelWorkbook>";
259 excelFile += "<x:ExcelWorksheets>";
260 excelFile += "<x:ExcelWorksheet>";
261 excelFile += "<x:Name>";
262 excelFile += "{worksheet}";
263 excelFile += "</x:Name>";
264 excelFile += "<x:WorksheetOptions>";
265 excelFile += "<x:DisplayGridlines/>";
266 excelFile += "</x:WorksheetOptions>";
267 excelFile += "</x:ExcelWorksheet>";
268 excelFile += "</x:ExcelWorksheets>";
269 excelFile += "</x:ExcelWorkbook>";
270 excelFile += "</xml>";
271 excelFile += "<![endif]-->";
272 excelFile += "</head>";
273 excelFile += "<body>";
275 excelFile += "</body>";
276 excelFile += "</html>";
278 var base64data = "base64," + $.base64.encode(excelFile);
279 window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data);
281 }else if(defaults.type == 'png'){
283 onrendered: function(canvas) {
284 var img = canvas.toDataURL("image/png");
290 }else if(defaults.type == 'pdf'){
292 var doc = new jsPDF('p','pt', 'a4', true);
293 doc.setFontSize(defaults.pdfFontSize);
296 var startColPosition=defaults.pdfLeftMargin;
297 $(el).find('thead').find('tr').each(function() {
298 $(this).filter(':visible').find('th').each(function(index,data) {
299 if ($(this).css('display') != 'none'){
300 if(defaults.ignoreColumn.indexOf(index) == -1){
301 var colPosition = startColPosition+ (index * 50);
302 doc.text(colPosition,20, parseString($(this)));
310 var startRowPosition = 20; var page =1;var rowPosition=0;
311 $(el).find('tbody').find('tr').each(function(index,data) {
314 if (rowCalc % 26 == 0){
317 startRowPosition=startRowPosition+10;
319 rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280);
321 $(this).filter(':visible').find('td').each(function(index,data) {
322 if ($(this).css('display') != 'none'){
323 if(defaults.ignoreColumn.indexOf(index) == -1){
324 var colPosition = startColPosition+ (index * 50);
325 doc.text(colPosition,rowPosition, parseString($(this)));
333 // Output as Data URI
334 doc.output('datauri');
339 function parseString(data){
341 if(defaults.htmlContent == 'true'){
342 content_data = data.html().trim();
344 content_data = data.text().trim();
347 if(defaults.escape == 'true'){
348 content_data = escape(content_data);