Help with updating attributes in FeatureLayer programmatically

4126
4
Jump to solution
11-10-2010 05:33 AM
JasonMielke
New Contributor II
I'm looking for a little help as I learn JavaScript and the ArcGIS API.  I've managed to develop an app that leverages the templatePicker and Editor Widget. 

But what I am struggling to do is update a couple of the feature's attributes either at the time the feature is added to the FeatureLayer or via a subsequent POST using the applyEdits update that fires after the user completes drawing the feature using the Editor Widget.  Currently I am attempting the later by listening for the "onEditsComplete" event, and then if it is an "addResults" type event I would like to access the graphic (that was just added) and to update a couple of it's attributes (e.g. feature creation date field = the current date).

some of the applicable code follows:
var map, polyFeatLayer

function init() {
    //... other code is in this function like the "onLayerAddResult" listener to initialize template picker/  editor widget, etc.
    
    polyFeatLayer = new esri.layers.FeatureLayer("http://www......../FeatureServer/0", {
        mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
        outFields: ,

        id: "polyFeatLayerId"
   });
   // note: currently I have not added corresponding ArcGISDynamicMapServiceLayer

    dojo.connect(polyFeatLayer, "onEditsComplete", addResultsComplete);
    dojo.connect(polyFeatLayer, "onSelectionComplete", selectionComplete);
}

function addResultsComplete(addResults) {
    if (addResults.length > 0) {
        alert("got Result"); //so far this works
        var featLyr = map.getLayer("polyFeatLayerId");
        
         var query = new esri.tasks.Query();
         query.where = "OBJECTID = '" + addResults[0].objectId + "'";
         featLyr.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
    }
}

function selectionComplete(features) {
    alert("count: " + features.length);  // this is 0, unless I close the map.InfoWindow and manually (re)select a feature -- so this is my first problem area ... ???
    if (features.length == 1) {
        var currentDate = new Date();
        var dateString = ((currentDate.getMonth() + 1) + "/" + currentDate.getDate() + "/" + currentDate.getFullYear());
        var fixedDate = new Date.parse(dateString);

        var polyDesc = document.forms[0].polyDescriptionInput.value;

        var feature = features[0];
        feature.attributes["DT_ADDED"] = fixedDate;
        feature.attributes["POLY_DESC"] = polyDesc;
        polyFeatLyr.applyEdits(null, [feature], null);   //and this fails too!!! with a  "TypeError: that._currentGraphic is undefined"
//


I really appreciate any help and direction with accomplishing this.

v/r, jason
0 Kudos
1 Solution

Accepted Solutions
JasonMielke
New Contributor II
Figured this out a few days back and during that process realized I was way overcomplicating it.  I'm replying here with the thought that this may be helpful to someone in the future.


So here it is...

var map, polyFeatLayer

function init() {
    //... other code is in this function like the "onLayerAddResult" listener to initialize template picker/  editor widget, etc.
    
    polyFeatLayer = new esri.layers.FeatureLayer("http://www......../FeatureServer/0", {
        mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
        outFields:
  • ,
  •         id: "polyFeatLayerId"    });     dojo.connect(polyFeatLayer, "onEditsComplete", addResultsComplete); } function addResultsComplete(addResults) {     if (addResults.length > 0) {         // this gets the corresponding graphic of the feature that was just added         var graphic = polyFeatLayer.graphics[polyFeatLayer.graphics.length - 1];         var currentDate = new Date();         var dateString = ((currentDate.getMonth() + 1) + "/" + currentDate.getDate() + "/" + currentDate.getFullYear());         var fixedDate = new Date.parse(dateString);         var polyDesc = document.forms[0].polyDescriptionInput.value;         graphic.attributes["DT_ADDED"] = fixedDate;         graphic.attributes["POLY_DESC"] = polyDesc;         polyFeatLayer.applyEdits(null, [graphic], null);    } } //





    jason

    View solution in original post

    4 Replies
    JasonMielke
    New Contributor II
    Figured this out a few days back and during that process realized I was way overcomplicating it.  I'm replying here with the thought that this may be helpful to someone in the future.


    So here it is...

    var map, polyFeatLayer
    
    function init() {
        //... other code is in this function like the "onLayerAddResult" listener to initialize template picker/  editor widget, etc.
        
        polyFeatLayer = new esri.layers.FeatureLayer("http://www......../FeatureServer/0", {
            mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
            outFields:
  • ,
  •         id: "polyFeatLayerId"    });     dojo.connect(polyFeatLayer, "onEditsComplete", addResultsComplete); } function addResultsComplete(addResults) {     if (addResults.length > 0) {         // this gets the corresponding graphic of the feature that was just added         var graphic = polyFeatLayer.graphics[polyFeatLayer.graphics.length - 1];         var currentDate = new Date();         var dateString = ((currentDate.getMonth() + 1) + "/" + currentDate.getDate() + "/" + currentDate.getFullYear());         var fixedDate = new Date.parse(dateString);         var polyDesc = document.forms[0].polyDescriptionInput.value;         graphic.attributes["DT_ADDED"] = fixedDate;         graphic.attributes["POLY_DESC"] = polyDesc;         polyFeatLayer.applyEdits(null, [graphic], null);    } } //





    jason
    AlexOliinyk
    New Contributor

    do you have real working demo ? I trying  a lot of difference examples no one doesn't work for me (

    0 Kudos
    JamesGonsoski
    New Contributor III
    Thank you so much for posting this solution. I've been struggling with this for days.
    0 Kudos
    satbirsingh
    New Contributor II

    Thanks you so much. This worked for me !!

    0 Kudos