I have an application that has filter widgets that interacts with the map on the page. The client would now like to be able to export the data to csv. I am a little tight in space I dont want nor need to see the data in a table. Has someone created a widget that does this? I dont think I found one yet but I will continue looking while I wait for responses.
Is there a hack? Ideally I just want a button that says export to csv.
Solved! Go to Solution.
function customExportCSV() {
let query = featureLayer.createQuery()
query.where = '1=1'
let csv = ''
let headers = ''
featureLayer.queryFeatures(query).then((featureSet) => {
headers = Object.keys(featureSet.features[0].attributes).join(',')
for (let i = 0; i < featureSet.features.length; i++) {
const data = Object.values(featureSet.features[i].attributes).join(',')
csv += data + '\n'
}
csv = headers + '\n' + csv
downloadCSV(csv)
})
}
function downloadCSV(csv) {
const csvFile = new Blob([csv], { type: 'text/csv' });
const downloadLink = document.createElement("a");
downloadLink.download = 'search_results.csv';
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
Here's my function for downloading a csv from a FeatureLayer.
function customExportCSV() {
let query = featureLayer.createQuery()
query.where = '1=1'
let csv = ''
let headers = ''
featureLayer.queryFeatures(query).then((featureSet) => {
headers = Object.keys(featureSet.features[0].attributes).join(',')
for (let i = 0; i < featureSet.features.length; i++) {
const data = Object.values(featureSet.features[i].attributes).join(',')
csv += data + '\n'
}
csv = headers + '\n' + csv
downloadCSV(csv)
})
}
function downloadCSV(csv) {
const csvFile = new Blob([csv], { type: 'text/csv' });
const downloadLink = document.createElement("a");
downloadLink.download = 'search_results.csv';
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
Here's my function for downloading a csv from a FeatureLayer.
This is great. Thanks!
This is assuming there is only one layer in the map. Correct? My map has multiple layers.
It will download data from whatever feature layer is assigned to the variable featureLayer. To make it more flexible, make featureLayer a parameter of the customExportCSV function, then create another function that finds a feature layer from a user input and calls customExportCSV(featureLayer).
Thanks. I am so so on JS, so I may hardcoded as you have it above at first, but I will work on the additional code to make it for flexible. Thank you for the outline of that part.