-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
data_table_3.5.js
101 lines (86 loc) · 3.31 KB
/
data_table_3.5.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
lizMap.events.on({
'uicreated': function () {
// Change the URL from your own data
const URL = 'https://url/index.php/lizmap/service/?repository=your_repositoryu&project=your_project&SERVICE=WFS&REQUEST=GetFeature&VERSION=your_version&TYPENAME=your_layerOUTPUTFORMAT=GeoJSON';
const NAME = "Nom de la couche"
var html = '<div id="statistic_content"></div>';
lizMap.addDock(
'table_dock',
`Table of ${NAME}`,
'right-dock',
html,
'icon-signal'
);
try {
const promise = getData(URL);
// Handle promise resolution
promise.then(features =>{
// Extract property names from the first feature
const propertyNames = Object.keys(features[0].properties);
const tableRows = [];
// Create table header using property names
const tableHeader = `
<thead>
<tr>
${propertyNames.map(propertyName => `<th>${propertyName}</th>`).join('')}
</tr>
</thead>
`;
// Add rows to the table
features.forEach(feature => {
const row = `
<tr>
${propertyNames.map(propertyName => `<td>${feature.properties[propertyName]}</td>`).join('')}
</tr>
`;
tableRows.push(row);
});
// Update the style of your table
const tableStyle = `
<style>
table#table-data {
width: 100%;
border-collapse: collapse;
border: 1px solid;
color:white;
}
#table-data th, #table-data td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
table#table-data th {
background-color: grey;
}
</style>
`;
// Get to the table container
const tableContainer = $('#statistic_dock');
// Add the content of the table
tableContainer.html(`
${tableStyle}
<table id="table-data">
${tableHeader}
${tableRows.join('')}
</table>
`);
})
}catch (error) {
console.error('An error occurred during processing: ', error);
}
}
})
// Asynchronous function to get data
async function getData(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP Error! Status: ${response.status}`);
}
const data = await response.json();
return data.features;
} catch (error) {
console.error('An error occurred while retrieving the data', error);
throw error;
}
};