ArcGIS javascript API Update Records of a Feature Layer

5857
8
Jump to solution
10-13-2013 07:55 PM
yufeizhuang
New Contributor
I'm new to ArcGIS javascript API, now I want to create a feature layer and then add an array of attributes or simply update the array of attributes of a specific field. I only need to replace the original values array as my own array. Is that possible? There's a method: attr(name,value), Can I use this? Can any one give me some hints or code? Thanks in advance!
0 Kudos
1 Solution

Accepted Solutions
JanelYang
New Contributor III
Hi,

You can use FeatureLayer.applyEdits(adds?,updates?,deletes?,callback?,errback?). To update the attributes of an existing feature, you need to provide the feature's object ID, create a new Graphic, and make an array of graphics as the input for the updateFeatures parameter. For example:

var attributes = {   OBJECTID: 12,   name: "My name" }; var feature = new Graphic(null, null, attributes);  var updateFeatures = []; updateFeatures.push(feature);  layer.applyEdits(null, updateFeatures, null, function(addResults, updateResults, deleteResults){   console.log('success', updateResults); }, function(error){   console.log('error', error); });


To add new features, use layer.applyEdits(addFeatures, null, null, ...) instead. The process of making an array of graphics as the addFeatures input is similar to the code snippet above, except you don't need to specify OBJECTID.

View solution in original post

0 Kudos
8 Replies
JanelYang
New Contributor III
Hi,

You can use FeatureLayer.applyEdits(adds?,updates?,deletes?,callback?,errback?). To update the attributes of an existing feature, you need to provide the feature's object ID, create a new Graphic, and make an array of graphics as the input for the updateFeatures parameter. For example:

var attributes = {   OBJECTID: 12,   name: "My name" }; var feature = new Graphic(null, null, attributes);  var updateFeatures = []; updateFeatures.push(feature);  layer.applyEdits(null, updateFeatures, null, function(addResults, updateResults, deleteResults){   console.log('success', updateResults); }, function(error){   console.log('error', error); });


To add new features, use layer.applyEdits(addFeatures, null, null, ...) instead. The process of making an array of graphics as the addFeatures input is similar to the code snippet above, except you don't need to specify OBJECTID.
0 Kudos
yufeizhuang
New Contributor
Thank you very much for your replying! I'm still confused how to specify the objectID and do I need to set the feature layer as editable before I can do any modifications?
0 Kudos
JanelYang
New Contributor III
I'm still confused how to specify the objectID

The objectID helps the feature server identify which existing feature you are updating. By default it's one of the attributes of the feature object. You can get it by querying a feature layer and include ObjectID in the outFields. Check out this sample. The code snippets in the description section might be what you need.

do I need to set the feature layer as editable before I can do any modifications?

Yes. You need to enable editing on your feature layer.
0 Kudos
yufeizhuang
New Contributor

Yes. You need to enable editing on your feature layer.


Thanks for your help, but how to set it editable? sorry I know it is a idiot question, but I'm really confused.
0 Kudos
JanelYang
New Contributor III
Thanks for your help, but how to set it editable? sorry I know it is a idiot question, but I'm really confused.

If your feature service is hosted on your arcgis.com account, go to the item page, click "edit", then scroll down to the "properties" section, modify the settings of editing:
[ATTACH=CONFIG]28489[/ATTACH][ATTACH=CONFIG]28490[/ATTACH]
If you want to do so with REST API, here is a reference you may look at: Update Definition
0 Kudos
yufeizhuang
New Contributor
I set it editable as the picture but when I test it using featureLayer.isEditable(), why it still returns false?
0 Kudos
JanelYang
New Contributor III
I set it editable as the picture but when I test it using featureLayer.isEditable(), why it still returns false?


All properties of a feature layer are loaded asynchronously. That is, you can only get correct values AFTER the feature layer is loaded.
Try placing featurelayer.isEditable() inside featurelayer.on("load"). It should reflect the editing setting of the feature layer correctly:

featurelayer.on("load", function(){
  console.log(featurelayer.isEditable());
});
0 Kudos
BertrandLAURANCIN
Esri Contributor

Hello,

Example 1 :

var map = new Map("map", {
basemap: "dark-gray",
zoom: 3,
});


layer = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0");

attributes = { objectid: 119369, typdamage: "Update successfully" };

feature = new Graphic( null, null, attributes );

map.addLayer(layer);

map.onLayerAddResult = () => {
layer.applyEdits(null, [feature], null,
function (add, update, del) { 
console.log('success', update);
}); 
};

Exemple 2 :

var map = new Map("map", {
basemap: "dark-gray",
zoom: 3,
});


layer = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0");

attributes = { objectid: 119369, typdamage: "Update successfully" };

feature = new Graphic( null, null, attributes );

map.addLayer(layer);

setTimeout(() => {

layer.applyEdits(null, [feature], null,
function (add, update, del) { 
console.log('success', update);
},
function (error) { 
console.log('error applyEdits \n', error);
});

}, 2000);

Bertrand

0 Kudos