This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
polymer-datatables-export.html
executable file
·72 lines (67 loc) · 1.84 KB
/
polymer-datatables-export.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<link rel="import" href="../polymer/polymer.html">
<polymer-element name="polymer-datatables-export" attributes="columns data filename exportTime tableApi">
<template></template>
<script src="../lodash/lodash.js"></script>
<script>
Polymer({
publish: {
filename: 'data-export',
data: [],
exportTime: null,
exportData: [],
exportAll: true,
},
attached: function() {
function loadScript(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.body.appendChild(script);
}
// Load Moment
if( typeof window.moment == 'undefined' ) {
loadScript("../moment/min/moment.min.js");
}
},
export: function(event, detail, sender) {
var poly = this;
poly.exportData = [];
if( ! poly.exportTime ) {
poly.exportTime = moment().format('-MM-DD-YY');
}
// Add titles
var titles = [];
var keys = [];
_.each(poly.columns, function(col) {
if( col.data ) {
keys.push( col.data );
titles.push( col.title || col.data );
}
});
// Add data for csv
poly.exportData.push(titles);
var data;
if( poly.exportAll ) {
data = _.clone(poly.data);
} else {
data = poly.tableApi.rows({
order: 'applied',
search: 'applied',
page: 'all',
}).data();
}
_.each( data, function(row) {
var d = [];
_.each(keys, function(key) {
var cellData = row[key] || '';
if (typeof cellData === 'string') {
cellData = cellData.replace(/,/g, '').replace(/^\s+|\s+$/g, '');
}
d.push( cellData );
});
poly.exportData.push(d);
});
},
});
</script>
</polymer-element>