Edit features (FeatureLayers ApplyEdits) - 4.3

2098
3
06-02-2017 02:51 AM
AndréRibeiro
New Contributor II

I tried to use this sample (Edit Features | ArcGIS API for JavaScript 4.3) but with some changes. Instead of use a portalItem I want to use a featureLayer. I already changed to my feature layer url and add the class to the require and to the function, they worked good.
My problem is when I click on a feature, instead of appear in the input boxes the attributes that are in the feature layer, appear the word "undefined" like in the picture below:

I can select and add new points successfully, that's why I think that isn't normal I can't fetch the attributes to my input boxes.
This is the code when I select a feature:

function selectFeature(objectId) {
           //quadrado
          // symbol for the selected feature on the view
          var selectionSymbol = SimpleMarkerSymbol({
            color: [0, 0, 0, 0],
            style: "square",
            size: '35px',
            outline: {
              color: [255, 96, 0, 0.66],
              width: 2
            }
          });

          var query = featureLayer.createQuery();
          query.where = featureLayer.objectIdField + " = " + objectId;

          featureLayer.queryFeatures(query).then(function(results) {
            if (results.features.length > 0) {
              editFeature = results.features[0];
              editFeature.symbol = selectionSymbol;
              view.graphics.add(editFeature);
            }
          });
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

And this is the code when I fetch the attribute results to the input boxes:

view.on("click", function(evt) {          
  unselectFeature();          
  view.hitTest(evt.screenPoint).then(function(response) {            
    if (response.results.length > 0 && response.results[0].graphic) {
      var feature = response.results[0].graphic;              
      selectFeature(feature.attributes[featureLayer.objectIdField]);                           
      inputDescription.value = feature.attributes["equipament"];
      inputUserInfo.value = feature.attributes["num_i_s"];                              
      form1.value = feature.attributes["lado"];                                  
      form2.value = feature.attributes["sustent"];                              
      form3.value = feature.attributes["braco"];
      form4.value = feature.attributes["botoneira"];                              
      form5.value = feature.attributes["av_acustic"];                              
      attributeEditing.style.display = "block";              
      updateInstructionDiv.style.display = "none";            
    }          
  });        
});


I already search a lot about this topic but I don't find anything that helps me. Anyone?

Thank you,
André Ribeiro

3 Replies
ThomasSolow
Occasional Contributor III

It may help to set the outFields parameter on your query to ["*"];

So:

var query = featureLayer.createQuery();
query.where = featureLayer.objectIdField + " = " + objectId;
query.outFields = ["*"];‍‍‍‍‍‍

This will ensure that the returned feature has all attribute fields.

However, it looks to me like you're trying to fill in the form based on the attributes already in the client.  In this code:

view.on("click", function(evt) {          
  unselectFeature();          
  view.hitTest(evt.screenPoint).then(function(response) {            
    if (response.results.length > 0 && response.results[0].graphic) {
      var feature = response.results[0].graphic;              
      selectFeature(feature.attributes[featureLayer.objectIdField]);                           
      inputDescription.value = feature.attributes["equipament"];
      inputUserInfo.value = feature.attributes["num_i_s"];                              
      form1.value = feature.attributes["lado"];                                  
      form2.value = feature.attributes["sustent"];                              
      form3.value = feature.attributes["braco"];
      form4.value = feature.attributes["botoneira"];                              
      form5.value = feature.attributes["av_acustic"];                              
      attributeEditing.style.display = "block";              
      updateInstructionDiv.style.display = "none";            
    }          
  });        
});
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The query is being sent in selectFeature(...) but then you immediately set the fields in the form.  This means you're going to be using the attributes that exist in the client before the query returns.  This isn't really a problem, assuming the client has the attributes.

What's the output if you add a console.log(feature.attributes) right after the selectFeature(...) line?  If the attributes are missing at this point, try changing your feature layer constructor:

var flayer = new FeatureLayer({
  url: <url>,
  outFields: ["*"]
});‍‍‍‍‍‍‍‍

And see if that helps.

If this doesn't help, you could try to set the form fields after the query has returned by moving that logic to the callback in selectFeatures.

AndréRibeiro
New Contributor II

The problem had been on the outFields on feature layer, the objectId was passed correctly, but don't fetch any result before because of that.

var flayer = new FeatureLayer({
  url: <url>,
  outFields: ["*"]
});

Thank you so much! So lame ahah

0 Kudos
by Anonymous User
Not applicable

Hey guys!

Do you happen to know how to make some of those inputUserInfo. items be uneditable. I want to have the app pass the information by itself and not allow users to modify the app's generated info

0 Kudos