|
| 1 | +lizMap.events.on({ |
| 2 | + 'uicreated': function () { |
| 3 | + |
| 4 | + // Change the URL from your own data |
| 5 | + 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'; |
| 6 | + const NAME = "Nom de la couche" |
| 7 | + |
| 8 | + var html = '<div id="statistic_content"></div>'; |
| 9 | + lizMap.addDock( |
| 10 | + 'table_dock', |
| 11 | + `Table of ${NAME}`, |
| 12 | + 'right-dock', |
| 13 | + html, |
| 14 | + 'icon-signal' |
| 15 | + ); |
| 16 | + |
| 17 | + try { |
| 18 | + const promise = getData(URL); |
| 19 | + |
| 20 | + // Handle promise resolution |
| 21 | + promise.then(features =>{ |
| 22 | + // Extract property names from the first feature |
| 23 | + const propertyNames = Object.keys(features[0].properties); |
| 24 | + const tableRows = []; |
| 25 | + |
| 26 | + // Create table header using property names |
| 27 | + const tableHeader = ` |
| 28 | + <thead> |
| 29 | + <tr> |
| 30 | + ${propertyNames.map(propertyName => `<th>${propertyName}</th>`).join('')} |
| 31 | + </tr> |
| 32 | + </thead> |
| 33 | + `; |
| 34 | + |
| 35 | + // Add rows to the table |
| 36 | + features.forEach(feature => { |
| 37 | + const row = ` |
| 38 | + <tr> |
| 39 | + ${propertyNames.map(propertyName => `<td>${feature.properties[propertyName]}</td>`).join('')} |
| 40 | + </tr> |
| 41 | + `; |
| 42 | + tableRows.push(row); |
| 43 | + }); |
| 44 | + |
| 45 | + // Update the style of your table |
| 46 | + const tableStyle = ` |
| 47 | + <style> |
| 48 | + table#table-data { |
| 49 | + width: 100%; |
| 50 | + border-collapse: collapse; |
| 51 | + border: 1px solid; |
| 52 | + color:white; |
| 53 | + } |
| 54 | + #table-data th, #table-data td { |
| 55 | + padding: 8px; |
| 56 | + text-align: left; |
| 57 | + border-bottom: 1px solid #ddd; |
| 58 | + } |
| 59 | + table#table-data th { |
| 60 | + background-color: grey; |
| 61 | + } |
| 62 | + </style> |
| 63 | + `; |
| 64 | + |
| 65 | + // Get to the table container |
| 66 | + const tableContainer = $('#statistic_dock'); |
| 67 | + |
| 68 | + // Add the content of the table |
| 69 | + tableContainer.html(` |
| 70 | + ${tableStyle} |
| 71 | + <table id="table-data"> |
| 72 | + ${tableHeader} |
| 73 | + ${tableRows.join('')} |
| 74 | + </table> |
| 75 | + `); |
| 76 | + |
| 77 | + }) |
| 78 | + }catch (error) { |
| 79 | + console.error('An error occurred during processing: ', error); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | +}) |
| 84 | + |
| 85 | +// Asynchronous function to get data |
| 86 | +async function getData(url) { |
| 87 | + try { |
| 88 | + const response = await fetch(url); |
| 89 | + |
| 90 | + if (!response.ok) { |
| 91 | + throw new Error(`HTTP Error! Status: ${response.status}`); |
| 92 | + } |
| 93 | + |
| 94 | + const data = await response.json(); |
| 95 | + return data.features; |
| 96 | + |
| 97 | + } catch (error) { |
| 98 | + console.error('An error occurred while retrieving the data', error); |
| 99 | + throw error; |
| 100 | + } |
| 101 | +}; |
0 commit comments