-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtable2csv.js
More file actions
53 lines (52 loc) · 1.75 KB
/
table2csv.js
File metadata and controls
53 lines (52 loc) · 1.75 KB
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
/**
* @description: Plugin to export HTML table to CSV file.
* @author: VenkataRamanaB
* @link: https://github.com/venkataramanab/table2csv
* Feel free to use or modify this plugin as far as my full name is kept
*/
(function ($) {
const _trim_text = (text) => {
return text.trim();
};
const _quote_text = (text) => {
return '"' + text + '"';
};
const _export = (lines, file_name) => {
const uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(lines.join('\n'));
const el_a = document.createElement('a');
el_a.href = uri;
el_a.download = file_name;
document.body.appendChild(el_a);
el_a.click();
document.body.removeChild(el_a);
};
const init = (tb, options) => {
let lines = [];
$(tb).find('thead>tr').each(function () {
let line = [];
$(this).find('th').each(function () {
line.push(_quote_text(_trim_text($(this).text())));
});
lines.push(line.splice(0).toString());
})
for (let i = 0; i < options.header_body_space; i++) lines.push('\n');
$(tb).find('tbody>tr').each(function () {
let line = [];
$(this).find('td').each(function () {
line.push(_quote_text(_trim_text($(this).text())));
});
lines.push(line.splice(0).toString());
})
_export(lines, options.file_name)
};
$.fn.extend({
table2csv: function (options) {
const default_options = {
file_name: 'table_records.csv',
header_body_space: 1
};
options = $.extend(default_options, options);
init(this, options);
}
})
})(jQuery);