Select to view content in your preferred language

Filter Data on Map and then Export Filtered Data to CSV

510
4
Jump to solution
04-05-2024 01:24 PM
RezaTehranifar2
New Contributor III

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.

0 Kudos
1 Solution

Accepted Solutions
JeffreyThompson2
MVP Regular Contributor
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.

GIS Developer
City of Arlington, Texas

View solution in original post

4 Replies
JeffreyThompson2
MVP Regular Contributor
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.

GIS Developer
City of Arlington, Texas
RezaTehranifar2
New Contributor III

This is great. Thanks!

This is assuming there is only one layer in the map. Correct? My map has multiple layers.

0 Kudos
JeffreyThompson2
MVP Regular Contributor

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).

GIS Developer
City of Arlington, Texas
0 Kudos
RezaTehranifar2
New Contributor III

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.

0 Kudos