[GIS] Update multiple features attributes in loop – JavaScript

arcgis-javascript-apiarcgis-servereditingfeature-layer

I am trying to update multiple features in a feature layer through a loop.

I have a feature layer that I would like to select and then assign someone to the features (through an attribute update. I can select the features and see all of the attributes but I can't figure out how to update the the features. See below for a sample of what I am trying to do. Right now it is a bit messy where I am trying to set the visibility of a form and button on after the selection so the user can choose an assignment.

Has anyone updated multiple features without using the attribute inspector or editor widget which I have only seen examples for editing a feature at a time.

 var map, geocoder, currentBaseMap, assignWindow, featureLayer;
 var locatorUrl ="http://gisaprd/ArcGIS/rest/services/SDE.AddressLocator_Comp/GeocodeServer";
 var selectedPoints = new Array();

require([
"esri/InfoTemplate",
"esri/map",
"esri/layers/FeatureLayer",
"esri/layers/ArcGISTiledMapServiceLayer",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/dijit/Geocoder",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/tasks/query",
"esri/toolbars/draw",
"dojo/dom",
"dojo/on",
"dojo/parser",
"dojo/_base/array",
"dojo/_base/Color",
"dijit/form/Button",
"dojo/domReady!"
 ],
 function (
 InfoTemplate, Map, FeatureLayer, ArcGISTiledMapServiceLayer, ArcGISDynamicMapServiceLayer,         Geocoder, SimpleFillSymbol, SimpleLineSymbol,
Query, Draw, dom, on, parser, arrayUtil, Color
 ) {

  parser.parse();

  var selectionToolbar, featureLayer;

  // Create the map
  map = new Map("map")

  aerialsLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://gisaprd/ArcGIS/rest/services/Aerials_4inch_12inch_Cached/MapServer", { "id": "Aerials", "visible": false });
  map.addLayer(aerialsLayer);

  streetsLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://gisaprd/ArcGIS/rest/services/SRSLandbase/MapServer", { "id": "Streets" });
  map.addLayer(streetsLayer);
  currentBaseMap = "Streets";

  map.on("load", initSelectToolbar);

  document.getElementById("AssignFrm").style.visibility = "hidden";
  document.getElementById("AssignBTN").style.visibility = "hidden";

  highlightSymbol = new esri.symbol.SimpleMarkerSymbol().setColor(new dojo.Color([255, 255, 0]));

  var content = "<b>Address</b>: ${SDE.GasServiceLocation.LOCATIONDESCRIPTION}";
  var infoTemplate = new InfoTemplate("${SDE.GasServiceLocation.CU_ID}", content);

  featureLayer = new FeatureLayer("http://gisadev:6080/arcgis/rest/services/ISS_Inspections/FeatureServer/0",
    {
        mode: FeatureLayer.MODE_ONDEMAND,
        infoTemplate: infoTemplate,
        outFields: ["*"]
    });

  featureLayer.setSelectionSymbol(highlightSymbol);
  featureLayer.on("selection-complete", createListForAssignment);
  //featureLayer.on("selection-clear", function () {
  //    dom.byId('messages').innerHTML = "<i>No Selected Fields</i>";
  //});
  map.addLayer(featureLayer);

  var myGeocoders = [{
      url: locatorUrl,
      name: "SDE.AddressLocator_Comp"
  }];
  geocoder = new Geocoder({
      map: map,
      autoComplete: true,
      arcgisGeocoder: false,
      geocoders: myGeocoders,
      value: "",
      zoomScale: 1200

  }, "search");
  geocoder.startup();

  on(dom.byId("selectFieldsButton"), "click", function () {
      selectionToolbar.activate(Draw.EXTENT);
  });
  on(dom.byId("clearSelectionButton"), "click", function () {
      featureLayer.clearSelection();
  });
  on(dom.byId("BaseMapToggle"), "click", ChangeBaseMap);
  //on(dom.byId("assignCrew"), "click", AssignInspectionCrew);

  function initSelectToolbar(event) {
      selectionToolbar = new Draw(event.map);
      var selectQuery = new Query();

      on(selectionToolbar, "DrawEnd", function (geometry) {
          selectionToolbar.deactivate();
          selectQuery.geometry = geometry;
          featureLayer.selectFeatures(selectQuery,
            FeatureLayer.SELECTION_NEW);          
     });
  }
  function ChangeBaseMap() {
      if (currentBaseMap == "Streets") {
          currentBaseMap = "Aerials";

          aerialsLayer.setVisibility(true);
      }
      else if (currentBaseMap == "Aerials") {
          currentBaseMap = "Streets";
          aerialsLayer.setVisibility(false);
      }
  }
  function createListForAssignment(event) {
      document.getElementById("AssignFrm").style.visibility = "visible";
      document.getElementById("AssignBTN").style.visibility = "visible";

      arrayUtil.forEach(event.features, function (feature, index) {
          selectedPoints.push(feature);
      });
   }
  });
  function assign() {
  if (document.getElementById('Jordan').checked) {
    x = document.getElementById('Jordan').value;
    for (var i = 0; i < selectedPoints.length; i++) {
        selectedPoints[i].attributes.ASSIGNED_TO = x;
    }
  }
  else {
    y = document.getElementById('Trenton').value;
    alert(y);
  }
  }

Best Answer

Welcome to GIS Stack Exchange. Thanks for sharing your code - a suggestion is to simplify this and only include the relevant parts of your code (rather than the entire application). In this case, that might be just the loop where you're trying to edit the feature layer.

Have you looked at applyEdits in the feature layer help file? This is one way to edit features programatically, and would be suitable for use within a JS loop.