-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
export-attributetable-csv_3.7.js
72 lines (61 loc) · 2.77 KB
/
export-attributetable-csv_3.7.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
lizMap.events.on({
'attributeLayerContentReady': function(e) {
const fieldSeparator = ',';
const fieldBoundary = '"';
const escapeChar = '"';
const reBoundary = new RegExp(fieldBoundary, 'g');
const newLine = navigator.userAgent.match(/Windows/) ? '\r\n' : '\n';
var join = function (a) {
var s = '';
// If there is a field boundary, then we might need to escape it in
// the source data
for (var i = 0, ien = a.length; i < ien; i++) {
if (i > 0) {
s += fieldSeparator;
}
s += fieldBoundary
? fieldBoundary +
('' + a[i]).replace(reBoundary, escapeChar + fieldBoundary) +
fieldBoundary
: a[i];
}
return s;
};
var exportData = function (cleanName) {
const tableSelector = '#attribute-layer-table-'+cleanName;
var oTable = $( tableSelector ).dataTable();
const heads = join($(tableSelector+' th[aria-controls="'+tableSelector.slice(1)+'"]').map(function() {return this.innerText}).toArray())
const rows = oTable.fnGetNodes().map(function(row) {
return join(Array.from(row.cells).slice(1).map(function(cell) {
if (cell.children.length != 0) {
const child = cell.children[0];
if (child.tagName == 'A') {
return child.href;
}
}
return cell.innerText;
}));
});
const output = heads + newLine + rows.join(newLine);
const charset = document.characterSet || document.charset;
const fileType = 'text/csv' + charset
const content = new Blob([output], { type: fileType });
var a = document.createElement('a');
a.download = cleanName+'.csv';
a.href = URL.createObjectURL(content);
a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
a.style.display = "none";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(() => { URL.revokeObjectURL(a.href); }, 1500);
}
const cleanName = lizMap.cleanName(e.featureType);
$('#attribute-layer-'+cleanName+' .attribute-layer-action-bar .export-formats').prepend(
'<button class="btn btn-mini exportCSV" data-name="'+cleanName+'">CSV</button>'
);
$('#attribute-layer-'+cleanName+' .attribute-layer-action-bar .export-formats .exportCSV').click(() => {
exportData(cleanName);
});
}
});