-
Notifications
You must be signed in to change notification settings - Fork 2
/
FeatureTable.html
296 lines (258 loc) · 10.4 KB
/
FeatureTable.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!--
Codepen sample: https://codepen.io/kellyhutchins/pen/QWVvGZq
-->
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
<title>
Layer List with table and export to csv
</title>
<link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/1.0.7/calcite.css" />
<link rel="stylesheet" href="https://js.arcgis.com/4.26/esri/themes/light/main.css" />
<script type="module" src="https://js.arcgis.com/calcite-components/1.0.7/calcite.esm.js"></script>
<script src="https://js.arcgis.com/4.26/"></script>
<style>
html,
body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
#viewDiv {
height: 100%;
width: 100%;
}
.table-container {
display: block;
height: 50vh;
position: fixed;
left: 0px;
right: 0px;
bottom: 20px;
z-index: 2;
margin-left: 0;
}
.table-content {
height: 50vh;
}
.hide {
display: none;
}
</style>
<script>
require([
"esri/WebMap",
"esri/views/MapView",
"esri/widgets/LayerList",
"esri/widgets/FeatureTable",
"esri/core/Collection",
"esri/core/reactiveUtils"
], function (WebMap, MapView, LayerList, FeatureTable, Collection, reactiveUtils) {
let table = null;
let layer = null;
// Containers that will hold the table
const container = document.getElementById("tablePanel");
const tableContentArea = document.getElementById("tableContent");
// Application events
container.addEventListener("calcitePanelClose", () => {
removeTable()
});
document.getElementById("exportCSV").addEventListener("click", () => {
const viewModel = table.viewModel;
const query = layer.createQuery();
query.geometry = view.extent;
query.returnGeometry = false;
// only show selected fields
let attributesToShow = [];
viewModel?.columns?.forEach((column) => {
if (!column?.hidden) {
attributesToShow.push(column?.name);
}
});
query.outFields = attributesToShow;
const oids = [];
viewModel.highlightIds?.map((item) => {
oids.push(item);
});
query.objectIds = oids;
// Sort the features based on the table sort order
const sortOrders = (table).activeSortOrders;
const sortOrder = sortOrders?.length > 0 ? sortOrders[0] : null;
if (sortOrder) {
const { direction, fieldName } = sortOrder;
query.orderByFields = [`${fieldName} ${direction?.toUpperCase()}`];
}
layer.queryFeatures(query).then((results) => {
const features = results?.features;
if (features?.length > 0) {
exportCSVData(features, layer);
}
});
}
)
const map = new WebMap({
portalItem: {
id: "390946e245e5402f823d3a62186c85aa"
}
});
const view = new MapView({
container: "viewDiv",
map
});
const layerList = new LayerList({
view,
// Add table action to the layer list
listItemCreatedFunction: (event) => {
const item = event.item;
if (item?.layer?.type !== "feature") {
return;
}
item.actionsSections = [
[
{
title: "Table",
className: "esri-icon-table",
id: "show-table"
}
]
];
}
});
layerList.on("trigger-action", (actionEvent) => {
// When the table button in the layer list is clicked
// open the table in the map
const { item, action } = actionEvent;
if (action.id === "show-table") {
layer = item.layer;
document.getElementById("tablePanel").heading = layer.title;
removeTable();
createTable();
}
});
view.ui.add(layerList, "top-right");
function removeTable() {
if (container) container.classList.add("hide");
if (container) container.setAttribute("closed", true);
table?.clearHighlights();
tableContentArea.innerHTML = null;
// Enable map popups when table is closed
view.popup.autoOpenEnabled = true;
}
function addTable() {
// Add the table to the map and open the panel
if (container) container.classList.remove("hide");
if (container) container.removeAttribute("closed");
// Disable map popups when table is open
view.popup.close();
view.popup.autoOpenEnabled = false;
}
function createTable() {
const tableDiv = document.createElement("div");
tableDiv.classList.add("manual-feature-table");
const hiddenFields = new Collection();
hiddenFields.add(layer?.objectIdField);
tableContentArea.appendChild(tableDiv);
table = new FeatureTable({
view,
columnReorderingEnabled: false,
layer,
hiddenFields,
container: tableDiv
});
addTable();
reactiveUtils.when(
() => view?.stationary,
() => {
if (view?.extent) table.filterGeometry = view.extent;
}, { initial: true }
);
view.on("immediate-click", (event) => {
if (container.classList?.contains("hide")) {
return;
}
// select table row when map is clicked
view.hitTest(event).then((response) => {
response?.results?.some((result) => {
if (result?.type === "graphic") {
// Only select the currently open table row
const graphic = result.graphic;
if (
graphic?.layer?.type === "feature" &&
graphic?.layer?.id === table?.layer?.id
) {
table.selectRows(graphic);
return true;
}
} else {
return false;
}
return result;
});
});
});
}
function exportCSVData(featureData, layer) {
// Add an export to csv option to the table menu
const data = [];
featureData?.forEach((feature, index) => {
if (index === 0) {
const rowKeys = [];
// add keys
for (const rk in feature?.attributes) {
let alias = rk;
layer?.fields?.some(field => {
if (field?.name === rk) {
if (field?.alias) { alias = field.alias };
return true;
}
});
rowKeys.push(alias);
}
data.push(rowKeys);
}
// get attributes
const values = [];
for (const key in feature?.attributes) {
values.push(feature.attributes[key]);
}
data.push(values);
});
const csvdata = convertArrayToCSV(data);
// download csv
const resultTitle = layer?.title || 'results';
downloadBlob(csvdata, `${resultTitle}.csv`, 'text/csv;charset=utf-8;');
}
function convertArrayToCSV(data) {
return data.map(row =>
row
.map(String) // convert every value to String
.map(v => v.replaceAll('"', '""')) // escape double colons
.map(v => `"${v}"`) // quote it
.join(',') // comma-separated
).join('\r\n'); // rows starting on new lines
}
function downloadBlob(content, filename, contentType) {
// Create a blob
var blob = new Blob([content], { type: contentType });
var url = URL.createObjectURL(blob);
// Create a link to download it
var csvDownload = document.createElement('a');
csvDownload.href = url;
csvDownload.setAttribute('download', filename);
csvDownload.click();
}
});
</script>
</head>
<body>
<div id="viewDiv"></div>
<calcite-panel class="table-container hide" id="tablePanel" closable closed>
<div slot="header-actions-end">
<calcite-action id="exportCSV" icon="file-csv" text="Export to CSV"></calcite-action>
</div>
<div id="tableContent" class="table-content"></div>
</calcite-panel>
</body>
</html>