Easiest way to customize AttributeInspector associated with an active Editor

3413
14
12-22-2010 12:46 PM
RoyceSimpson
Occasional Contributor III
I've got an app with Editor and default AttributeInspector but would like to basically do what this Attribute Inspector (edit) sample does in terms of a custom AttributeInspector.  However, the sample only updates existing features and isn't wired to an Editor. 

Is there a way to simply point existing Editor to a custom AttributeInspector?

I would like to remove (or possibly just disable) a couple fields from the Attribute Inspector and populate those programatically when a new feature is created or when an existing feature is updated.
Tags (2)
0 Kudos
14 Replies
MehulChoksey
Esri Contributor
Royce,

You could get hold of the existing attribute inspector associated with the editor and assign your own "field" inspector to it.
Below is the code snippet. Hope this helps.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:esri="http://www.esri.com/2008/ags"
               initialize="application1_initializeHandler(event)"
               pageTitle="Using the Editor component">
    <!--
    This sample shows you how to use the editor component.
    -->
    
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
        @namespace esri "http://www.esri.com/2008/ags";
        @namespace components "com.esri.ags.components.*";
        @namespace supportClasses "com.esri.ags.components.supportClasses.*";
        
        components|InfoWindow
        {
            background-color: #FFFFFF;
            border-thickness: 2;
        }
    </fx:Style>
    
    <fx:Script>
        <![CDATA[
            import com.esri.ags.components.AttributeInspector;
            import com.esri.ags.components.supportClasses.FieldInspector;
            
            import component.infoWindow;
            
            import mx.events.FlexEvent;
            
            protected function application1_initializeHandler(event:FlexEvent):void
            {
                myEditor.featureLayers = [ incidentsAreas ];
                var fieldInspector1:FieldInspector = new FieldInspector();
                fieldInspector1.featureLayer = incidentsAreas;
                fieldInspector1.fieldName = "description";
                fieldInspector1.label="DESCRIPTION"
                                
                var rendererFactory:ClassFactory = new ClassFactory(MYFieldRenderer);
                var fieldInspector2:FieldInspector = new FieldInspector();
                fieldInspector2.featureLayer = incidentsAreas;
                fieldInspector2.fieldName = "ftype";
                fieldInspector2.label="MyCustomComponent"
                    
                fieldInspector2.renderer = rendererFactory;
                var attributeInspector: AttributeInspector = myEditor.attributeInspector;
                attributeInspector.featureLayers = [incidentsAreas]
                attributeInspector.fieldInspectors = [fieldInspector2,fieldInspector1]
            }
        ]]>
    </fx:Script>
    
    <fx:Declarations>
        <esri:GeometryService id="myGeometryService" url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"/>
    </fx:Declarations>
    
    <esri:Map id="myMap">
        <esri:extent>
            <esri:Extent id="socal"
                         xmin="-13471000" ymin="3834000" xmax="-12878000" ymax="4124000">
                <esri:SpatialReference wkid="102100"/>
            </esri:Extent>
        </esri:extent>
        <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
        <esri:FeatureLayer id="incidentsAreas"
                           mode="snapshot"
                           outFields="[ftype,description]"
                           url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer/2"/>
    </esri:Map>
    
    <esri:Editor id="myEditor"
                 width="100%" height="200"
                 geometryService="{myGeometryService}"
                 map="{myMap}"/>
</s:Application>



Code for MYFieldRenderer:
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" 
          xmlns:esri="http://www.esri.com/2008/ags" width="100%" height="100%">
    <s:Button label="Custom Field Renderer"/>
</s:HGroup>



0 Kudos
RoyceSimpson
Occasional Contributor III
Thanks.

I'll give it a try.  Looks like the piece I was looking for was this, Which certainly seems straightforward enough:

               var attributeInspector: AttributeInspector = myEditor.attributeInspector;
                attributeInspector.featureLayers = [incidentsAreas]
                attributeInspector.fieldInspectors = [fieldInspector2,fieldInspector1]


Basically, just grab a reference to the default attributeInspector and reassign some new FieldInspectors.
Nice.

Will post back with results.
-r
0 Kudos
RoyceSimpson
Occasional Contributor III
Mehul, that worked great.  I was able to remove a couple fields from the AttributeInspector using similar method to your example... setting the visible property on the fieldInspectors to false.

Now, I'd like to programmatically populate the values of those "hidden" fields of newly created features before they get committed back to the database.  What would be your suggestion for doing so?

Thanks,
-Royce
0 Kudos
RoyceSimpson
Occasional Contributor III
Mehul, that worked great.  I was able to remove a couple fields from the AttributeInspector using similar method to your example... setting the visible property on the fieldInspectors to false.

Now, I'd like to programmatically populate the values of those "hidden" fields of newly created features before they get committed back to the database.  What would be your suggestion for doing so?

Thanks,
-Royce


Specifically, I've got users logging into the app and I have a field in the editable features that I'd like to pre-populate with the user's ID.  I don't see a sample that does this kind of programmatic field population when new features are added.  Has anyone performed this successfully?
0 Kudos
MehulChoksey
Esri Contributor
Royce,

Modify the attribute of the selected graphic on the Featurelayer's selectioncompleteEvent handler:
e.g.
protected function incidentsAreas_selectionCompleteHandler(event:FeatureLayerEvent):void
            {
                Graphic(event.features[0]).attributes['description']  = "MyDescription"
            }
0 Kudos
RoyceSimpson
Occasional Contributor III
Royce,

Modify the attribute of the selected graphic on the Featurelayer's selectioncompleteEvent handler:
e.g.
protected function incidentsAreas_selectionCompleteHandler(event:FeatureLayerEvent):void
            {
                Graphic(event.features[0]).attributes['description']  = "MyDescription"
            }


Thanks Mehul,
I've done the above and have verified that the attributes are being populated on the new feature/graphic (both in the AttributeInspector as well as inspecting the event.features[0].attributes property during runtime, but the values never get committed back to the server.  The user entered values do, just not the prepopulated ones.

Is there something I need to do after setting the attribute values on the graphic but before the feature is committed back to the server to get them to persist on the features?
0 Kudos
RoyceSimpson
Occasional Contributor III
I'm having good success with:

fl.addEventListener(FeatureLayerEvent.EDITS_STARTING, editsStartingHandler);

private function editsStartingHandler(event:FeatureLayerEvent):void
   {
    if (event.adds)
    {
     event.adds[0].attributes['EDITEDBY']  = ...
                                        ...
0 Kudos
PhilipThompson
New Contributor II
Thanks for updating your thread. For someone with not much Flex experience this as saved me a lot of hassle.
0 Kudos
PhilipThompson
New Contributor II
As a matter of interest, which login mechanism did you use and where did you hold that information till it was needed at event.adds[0].attributes['EDITEDBY']  = ...
0 Kudos