This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
datatable.exportable.min.js
23 lines (23 loc) · 5.65 KB
/
datatable.exportable.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*! Exportable 0.0.10
* © 2017 Karl Saunders
*/
/**
* @summary Exportable
* @description Vanilla-DataTables extension to allow for exporting to various formats
* @version 0.0.10
* @file datatable.exportable.js
* @author Karl Saunders
* @contact [email protected]
* @copyright Copyright 2017 Karl Saunders
*
*
* This source file is free software, available under the following license:
* MIT license - https://github.com/Mobius1/Vanilla-DataTables/blob/master/LICENSE
*
* This source file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
*
* For details please refer to: https://github.com/Mobius1/Vanilla-DataTables
*/
window.DataTable&&DataTable.extend("exportable",function(e,t,n){var i={download:!0,skipColumns:[],escapeHTML:!0,lineDelimiter:"\n",columnDelimiter:",",includeHeadings:!0,columnize:!1,paddingCharacter:" ",tableName:"table",replacer:null,space:4,rootNode:"Root",childNode:"Child",modal:!0},o=function(){};return o.prototype.init=function(){this.initialised||(this.config=n.extend(i,t),this.initialised=!0)},o.prototype.getRows=function(t){var i=[];return(t=t||this.config).pages?isNaN(t.pages)?Array.isArray(t.pages)&&n.each(t.pages,function(t){i=i.concat(e.pages[t-1])}):i=i.concat(e.pages[t.pages-1]):i=i.concat(e.table.rows),i},o.prototype.getConfig=function(e){return e&&n.isObject(e)?n.extend(n.extend({},this.config),e):this.config},o.prototype.stripHTML=function(e){return e.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?(\/)?>|<\/\w+>/gi,"")},o.prototype.parseXMLTag=function(e){return(e=e.charAt(0).toUpperCase()+e.slice(1)).replace(/\.|\s+/g,"")},o.prototype.export=function(e){switch((e=this.getConfig(e)).type.toLowerCase()){case"json":this.toJSON(e);break;case"sql":this.toSQL(e);break;case"csv":this.toCSV(e)}},o.prototype.columnize=function(e,t){var i=[];return n.each(e.strings,function(o,a){i[a]=[],n.each(o,function(n,o){if(i[a][o]=n,n.length<e.lengths[o])for(var c=e.lengths[o]-n.length,s=0;s<c;s++)i[a][o]+=t})}),i},o.prototype.toText=function(t){(t=this.getConfig(t)).type="txt";var i="",o=[],a=t,c=e.table,s=this.getRows(a),l=[],r=[];return t.includeHeadings&&s.unshift(c.header),n.each(s,function(e,o){if(t.columnize&&!r.length)for(var a=0;a<e.cells.length;a++)r[a]=0;l[o]=[],n.each(e.cells,function(e,n){i=e.content.trim(),l[o][n]=i,t.columnize&&i.length>r[n]&&(r[n]=i.length)})}),o=t.columnize?this.columnize({lengths:r,strings:l},t.paddingCharacter):l,i="",o.forEach(function(e){i+=e.join(t.columnDelimiter).trim()+"\n"}),a.escapeHTML&&(i=this.stripHTML(i)),a.download&&(i="data:text/plain;charset=utf-8,"+i,this.download(i,t)),i},o.prototype.toCSV=function(t){(t=this.getConfig(t)).type="csv";var i=[],o=[],a=t,c=e.table,s=this.getRows(t);return t.includeHeadings&&(n.each(c.header.cells,function(e){!e.hidden&&a.skipColumns.indexOf(e.index)<0&&(i+=e.content+a.columnDelimiter)}),i=i.trim().substring(0,i.length-1),i+=a.lineDelimiter),n.each(s,function(e,t){o[t]=o[t]||{},n.each(e.cells,function(e,t){!e.hidden&&a.skipColumns.indexOf(e.index)<0&&(i+=e.content+a.columnDelimiter)}),i=i.trim().substring(0,i.length-1),i+=a.lineDelimiter}),i=i.trim().substring(0,i.length-1),a.escapeHTML&&(i=this.stripHTML(i)),a.download&&(i="data:text/csv;charset=utf-8,"+i,this.download(i,t)),i},o.prototype.toJSON=function(t){(t=this.getConfig(t)).type="json";var i="",o=[],a=t,c=e.table,s=this.getRows(t);return n.each(s,function(e,t){o[t]=o[t]||{},n.each(e.cells,function(e,n){!e.hidden&&a.skipColumns.indexOf(e.index)<0&&(o[t][c.header.cells[e.index].content]=s[t].cells[e.index].content)})}),i=JSON.stringify(o,a.replacer,a.space),a.escapeHTML&&(i=this.stripHTML(i)),a.download&&(i="data:application/json;charset=utf-8,"+i,this.download(i,t)),i},o.prototype.toSQL=function(t){(t=this.getConfig(t)).type="sql";var i=t,o=e.table,a=this.getRows(t),c="INSERT INTO `"+i.tableName+"` (";return n.each(o.header.cells,function(e){!e.hidden&&i.skipColumns.indexOf(e.index)<0&&(c+="`"+e.content+"`,")}),c=c.trim().substring(0,c.length-1),c+=") VALUES ",n.each(a,function(e){c+="(",n.each(e.cells,function(e){!e.hidden&&i.skipColumns.indexOf(e.index)<0&&(c+="`"+e.content+"`,")}),c=c.trim().substring(0,c.length-1),c+="),"}),c=c.trim().substring(0,c.length-1),c+=";",i.escapeHTML&&(c=this.stripHTML(c)),i.download&&(c="data:application/sql;charset=utf-8,"+c,this.download(c,t)),c},o.prototype.toXML=function(t){(t=this.getConfig(t)).type="xml";var i,o=this,a=t,c=e.table,s=o.getRows(t),l='<?xml version="1.0" encoding="UTF-8"?><{root}>';return n.each(s,function(e){l+="<{child}>",n.each(e.cells,function(e){i=o.parseXMLTag(c.header.cells[e.index].content),l+="<"+i+">"+(a.escapeHTML?o.stripHTML(e.content):e.content)+"</"+i+">"}),l+="</{child}>"}),l+="</{root}>",l=l.replace(/\{root\}/g,t.rootNode),l=l.replace(/\{child\}/g,t.childNode),a.download&&(l="data:text/xml;charset=utf-8,"+l,this.download(l,t)),l},o.prototype.download=function(e,t){if(e){var n=t.filename||"datatable_export";n+="."+t.type,e=encodeURI(e);var i=document.createElement("a");i.href=e,i.download=n,document.body.appendChild(i),i.click(),document.body.removeChild(i)}},o.prototype.print=function(t){t&&n.isObject(t)&&(t=n.extend(this.config,t));var i=document.createElement("table"),o=document.createElement("thead"),a=document.createElement("tbody");i.appendChild(o),i.appendChild(a),n.each(e.table.header.cells,function(e){o.appendChild(e.node.cloneNode(!0))}),n.each(e.table.rows,function(e){a.appendChild(e.node.cloneNode(!0))});var c=window.open();c.document.body.appendChild(i),t.modal&&(c.focus(),c.print())},o.prototype.destroy=function(){this.initialised&&(this.initialised=!1)},new o});