Select to view content in your preferred language

On Edit Complete

5583
9
Jump to solution
11-16-2015 09:20 AM
MeleKoneya
Frequent Contributor

I am using the Edit Widget and the Batch Attribute Editor widget to edit our Feature Layer.     I would like to be able to modify two attributes when the Edit is Complete with these widgets.    I would like to update a date field and a user field.    I know this can be done via the Editor Tracking Fields,  but the layer is edited in ArcMap and I only want to update the attributes when an edit occurs via WAB.     Is there a place in WAB where I can put some custom code to set these attributes when an edit is compete?

Thanks,

Mele

0 Kudos
1 Solution

Accepted Solutions
LizDaRos
Regular Contributor

Mele,

I asked this same kind of question a few weeks ago and Robert suggested adding these lines to the _worksAfterCreate: function which is in the Edit Widget.js.

//Add the autopopulate field value

        this.editor.templatePicker.on("selection-change", lang.hitch(this, function() {

          var selected = this.editor.templatePicker.getSelected();

          if (selected) {

            var featureLayer = selected.featureLayer;

            on.once(featureLayer, "before-apply-edits", lang.hitch(this, function(evt){

              if(evt.adds && evt.adds.length > 0){

                if(evt.adds[0].attributes.hasOwnProperty('Begin_Date_Occ')){

                  evt.adds[0].attributes.Begin_Date_Occ = new Date(Date.now());

                }

                this.editor.attributeInspector.refresh();

              }

            }));

          }

        }));

//End add

This only works when creating new features so if you are just updating attributes of already created features these fields will not auto-populate, I'm still trying to fix that. Maybe you could remove the this.editor.attributeInspector.refresh(); line so it does not show the auto-populated fields.

Here's the link to my question for more information.

View solution in original post

9 Replies
LizDaRos
Regular Contributor

Mele,

I asked this same kind of question a few weeks ago and Robert suggested adding these lines to the _worksAfterCreate: function which is in the Edit Widget.js.

//Add the autopopulate field value

        this.editor.templatePicker.on("selection-change", lang.hitch(this, function() {

          var selected = this.editor.templatePicker.getSelected();

          if (selected) {

            var featureLayer = selected.featureLayer;

            on.once(featureLayer, "before-apply-edits", lang.hitch(this, function(evt){

              if(evt.adds && evt.adds.length > 0){

                if(evt.adds[0].attributes.hasOwnProperty('Begin_Date_Occ')){

                  evt.adds[0].attributes.Begin_Date_Occ = new Date(Date.now());

                }

                this.editor.attributeInspector.refresh();

              }

            }));

          }

        }));

//End add

This only works when creating new features so if you are just updating attributes of already created features these fields will not auto-populate, I'm still trying to fix that. Maybe you could remove the this.editor.attributeInspector.refresh(); line so it does not show the auto-populated fields.

Here's the link to my question for more information.

AlejandroCosci
Deactivated User

Hello Mele;

Just a quick question...

I have same prom like you, I need update same fields during edit occurs.

Did you solve this issue?

Regards,

Alejandro Cosci

0 Kudos
MeleKoneya
Frequent Contributor

Alejandro,

I did solve this issue using the code that Liz provided which Robert had suggested.

Let me know if you have any questions on how I implemented this.

Mele

AlejandroCosci
Deactivated User

Thanks for your quick answer Mele!

Yes I had some problems to adapt the solution of Liz to my project.

As the USR draws a polygon, I need to update a field with a global var to populate a specific field(USR_ID).

If you have an example for this I would really appreciate it.

Regards,

Alejandro

0 Kudos
MeleKoneya
Frequent Contributor

Alejandro,

This my complete code.   

_worksAfterCreate: function () {
       //debugger;
        this.editor.templatePicker.on("selection-change", lang.hitch(this, function() { 
          var selected = this.editor.templatePicker.getSelected(); 
          if (selected) { 
            var featureLayer = selected.featureLayer;
            on.once(featureLayer, "before-apply-edits", lang.hitch(this, function(evt){ 
            if(evt.adds && evt.adds.length > 0){ 
                if(evt.adds[0].attributes.hasOwnProperty('sfd_status')){ 
                  evt.adds[0].attributes.sfd_status = "NOT FLUSHED";
                }
                if (evt.adds[0].attributes.hasOwnProperty('sfd_edit_by')){ 
                  evt.adds[0].attributes.sfd_edit_by = StationShift;
                } 
                 if (evt.adds[0].attributes.hasOwnProperty('sfd_edit_date')){ 
                  evt.adds[0].attributes.sfd_edit_date = convertUTCDateToLocalDate(new Date(Date.now()));
                }
                refreshLayers(this.map, this.map.graphicsLayerIds);
                this.editor.attributeInspector.refresh();
              } 
            }));
           } 
        }));
       },

The section that may be most applicable to your situation is where I have a field called  'sfd_edit_by' that I am populating with my 'StationShift' variable defined elsewhere.

if (evt.adds[0].attributes.hasOwnProperty('sfd_edit_by')){ 
          evt.adds[0].attributes.sfd_edit_by = StationShift;
   } 
AlejandroCosci
Deactivated User

Thanks for your help Mele!

After several test we could use the code that you provide me.

But with some changes:

I added the code into _addButtonToInspector: function() not into _worksAfterCreate: function()

And remove the line:

refreshLayers(this.map, this.map.graphicsLayerIds);  It did not work on my code.

Again, thanks a lot for your help!

Alejandro Cosci

MeleKoneya
Frequent Contributor

Alejandro,

I am grateful to Liz and Robert for their help and I am glad their assistance to me was also of use to you.

Mele

Robert Scheitlin, GISP

JoseSanchez
Frequent Contributor

Hello everyone,

I am also using the Edit Widget and I need to autopopulate fields in my code.

The application creates a new polygon layer with 2 variations: one blue and another green.

To start I just added to the source code  ..\widgets\Edit\Widget.js and 2 alerts

on the events:

   _worksAfterCreate: Alert("_worksAfterCreate") 

_worksBeforeCreate: Alert("_worksAfterCreate")

Both alerts display before the Edit Widget window show all its components. Before the drawing tools part to select a polygons are visible inside Edit Widget window.

Also I just added the code from this post to update one field but it does not work

This is my question:

After selecting a shape to draw a new polygon with the  Edit Widget it pops up a new window to enter attributes (attribute inspector) . I have to click on the button close in the attribute inspector to save all the values entered on the attributes, but the event _worksAfterCreated is not called from the Attribute Inspector anymore.

_worksAfterCreate: function(settings) {

        // add close button to atiInspector

        this._addButtonToInspector();

 

        // disable delete button in the toolbar

        if(this._configEditor.toolbarVisible) {

          this._disableDeleteBtnInToolbar();

        }

 

        // resize editPopup

        this.editPopup.resize(300, 251);

        // update templatePicker for responsive.

        this.editor.templatePicker.update(true);

        // // reset default selectionSymbol that change by Editor dijit.

        // array.forEach(this.editor.settings.layerInfos, function(layerInfo) {

        //   layerInfo.featureLayer.setSelectionSymbol();

        // }, this);

 

                                //Customized Code for the AutoPopulated fields

                               

                                alert("_worksAfterCreate ");

                                this.editor.templatePicker.on("selection-change", lang.hitch(this, function() {

 

          var selected = this.editor.templatePicker.getSelected();

 

          if (selected) {

 

            var featureLayer = selected.featureLayer;

 

            on.once(featureLayer, "before-apply-edits", lang.hitch(this, function(evt){

 

              if(evt.adds && evt.adds.length > 0){

 

                if(evt.adds[0].attributes.hasOwnProperty('ID')){

 

                  evt.adds[0].attributes.ID = 1);

 

                }

 

                this.editor.attributeInspector.refresh();

 

              }

 

            }));

 

          }

 

        }));

0 Kudos
JoseSanchez
Frequent Contributor

Just to let you know that the code works  in the _worksAfterCreate:  event.

0 Kudos