export featuretable to csv exports only one field and objectid

75
2
Jump to solution
08-10-2022 02:18 PM
LefterisKoumis
Regular Contributor

I use Rene's example posted at: https://codepen.io/odoe/pen/rqXqVw?editors=0010

to export featuretable to csv but it produces a csv file with 2 fields including the objectid.

I first selected the features by the geometry.

LefterisKoumis_0-1660165724407.png

Then the feature table is updated to show selected features.

LefterisKoumis_2-1660165866696.png

 

Then clicked on the export button to create the csv file but only one field  but records span across multiple fields!

LefterisKoumis_3-1660165931818.png

As Rene's example shows, first declare the variable

 

let resultFeatures

 

get the selected features.

 

 featureLayerView
            .queryFeatures(query)
            .then((results) => {
              const graphics = results.features;
              resultFeatures = graphics;

 

THen, attach code to the export button:

 

view.ui.add("btn-export", "top-left");
      const btn = document.getElementById("btn-export");
      btn.addEventListener("click", () => {

        if (resultFeatures.length) {          
          // export to csv
          const attrs = resultFeatures.map(a => a.attributes);
          console.log(attrs)
          const headers = {};
          const entry = attrs[0];
          for (let key in entry) {
            if (entry.hasOwnProperty(key)) {
              headers[key] = key;
            }
          }
          exportCSVFile(headers, attrs, "export");
        }
      });

 

The issue by now is exposed because one field is passed on to the CSV. Why?

0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor II

That's an old sample. I would try a couple of things.

  • Set outFields of layer to ["*"]
  • Or set outFields of query to ["*"]

I suspect the fields are not in the query results, so you need to ask for them all.

View solution in original post

0 Kudos
2 Replies
ReneRubalcava
Frequent Contributor II

That's an old sample. I would try a couple of things.

  • Set outFields of layer to ["*"]
  • Or set outFields of query to ["*"]

I suspect the fields are not in the query results, so you need to ask for them all.

0 Kudos
LefterisKoumis
Regular Contributor

Thank you. I already had the query to outFields=["*"] but not the layer. Apparently, by not using the same for the layer it does not by default use all fields even though the featuretable displays all fields.

0 Kudos