adding a graphic to a featurelayer

3172
4
10-07-2010 06:18 AM
KathleenBrenkert
Occasional Contributor
I have passed the resulting graphic from my address locator to my FeatureLayer and it successfully adds a point to the database.  My next step would be to enter some attributes but I can't quite figure out the best way to do it. 
I found 1 post that suggested looping through the featurelayer's layerinfo.fields to get the fields and set them to my graphic's attribute collection.  Would anyone have an example I can work from?  Other ideas?
Tags (2)
0 Kudos
4 Replies
ReneRubalcava
Frequent Contributor
LayerDetails has a fields element, which I think contains a Field object.

So what you could do is something like this pseudocode

var ld:LayerDetails = fLayer.layerDetails;
var field:Field;
for each (field in ld.fields) {
    // probably loop through graphics
    var g:Graphic;
    for each (g in fLayer.graphicsProvider) {
        g.attributes[field.name] = "somedata"; // probably some if/else/switch to grab address info
    }
}


That could be a start, you'd just need to get your data to the right fields.
Either way, you should be able to find Field names/aliases/types in fLayer.layerDetails.fields.
0 Kudos
KathleenBrenkert
Occasional Contributor
That got me started.  I added this code to my application and I'm obviously missing an array, I get the very last field in my database assigned to my graphic.  However, after inserting this code myGraphic no longer gets added as a point to my featureLayer.
var ld:LayerDetails=myFeatureLayer.layerDetails;
var field:Field;
for each (field in ld.fields)
{
 myGraphic.attributes=field.name;
}
myFeatureLayer.applyEdits([myGraphic], null, null);


I'm not sure I'm approaching this right, I want to edit my feature layer without using Editor or TemplatePicker.  I need for the point to be added where the address is located, so using the resulting graphic from my locator is perfect.  But how to edit the attribute information from here is not clear.  I only "think" I need to pass the fields to my graphic.  The post I read gave me the impression I would then have access to the attributeInspector.  I would like to pass the address from my locator to a field in the database, but every other attribute will be entered by my user.
0 Kudos
DasaPaddock
Esri Regular Contributor
This sample may help.
http://help.arcgis.com/en/webapi/flex/samples/index.html?sample=AttributeInspectorInfoWindowEditable

One issue in the code above is that the attributes should be an Object of key-value pairs. You may also want to try using a tool like HttpFox to see what the request and response from the server is when you call applyEdits().
0 Kudos
KathleenBrenkert
Occasional Contributor
Many thanks to you both, after revisiting my code with your suggestions I realized I was looking at it wrong.  I was trying to pull my field names from my feature layer to assign them to my graphic, but that was not necessary.  All I had to do was assign attributes to my graphic that matched the field names in my feature layer.  I also added a basic form and then assigned each attribute a value from one of my form items.  sample of code below.

g.attributes={Field1: formitem1.text, Field2: formitem2.text}  
myFeatureLayer.applyEdits(, null, null);

0 Kudos