-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_to_csv.js
79 lines (67 loc) · 2.06 KB
/
table_to_csv.js
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
73
74
75
76
77
78
79
// Generated by CoffeeScript 1.4.0
(function() {
var TableToCSV, ttcsv;
TableToCSV = (function() {
function TableToCSV() {}
TableToCSV.prototype.data = "";
TableToCSV.prototype.init = function() {
var table, _i, _len, _ref, _results;
_ref = $('[data-table-to-csv]');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
table = _ref[_i];
_results.push(this.add_button(table));
}
return _results;
};
TableToCSV.prototype.add_button = function(table) {
var button,
_this = this;
button = $('<button type="button" class="btn pull-right" id="missing-csv-download">CSV<i class="icon-download-alt"></i></button>');
$(table).before(button);
return button.click(function(evt) {
return _this.parse(table);
});
};
TableToCSV.prototype.parse = function(table) {
var blob, row, _i, _len, _ref;
this.data = "";
this.parse_row(table.tHead.rows[0]);
_ref = table.tBodies[0].rows;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
row = _ref[_i];
this.parse_row(row);
}
blob = new Blob([this.data], {
type: "application/octet-stream;charset=utf-8"
});
return saveAs(blob, table.getAttribute('data-table-to-csv'));
};
TableToCSV.prototype.parse_row = function(row) {
var cell, cells;
cells = (function() {
var _i, _len, _ref, _results;
_ref = row.cells;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
cell = _ref[_i];
_results.push(cell.innerHTML);
}
return _results;
})();
cells = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = cells.length; _i < _len; _i++) {
cell = cells[_i];
_results.push('"' + cell + '"');
}
return _results;
})();
return this.data += cells.join(',') + "\n";
};
return TableToCSV;
})();
ttcsv = new TableToCSV;
ttcsv.init();
}).call(this);