This is a continuation from a few other threads-Re: Combine line and point feature classes attributes How to query features and populate a line graph in bottom pane with analysis
We are trying to figure out how to access a feature attribute from a "simple click". In the past I was accessing a feature attribute by assigning it a variable, ex. hydrantRecord = evt.graphic.attributes.HydrantID . I did this after a selection was made with the attribute inspector and before it went to a series of relationship queries.
So the question is, what is the simplest way to access a feature attribute and assign it to a variable without popups or info windows?
Thanks.
Solved! Go to Solution.
William,
I'm not sure I completely understand the scenario either - and it looks like Robert also hit on this point - but you can set up a map click event to grab the feature's graphic, which contains its attributes, then send the desired attribute value to your query.
For example, something like this:
map.on("click", function(evt){ //if you click a graphic, grab the desired attribute if(evt.graphic){ var yourVariable = evt.graphic.attributes.FIELDNAME; } else{ return; } //send yourVariable to queryTask });
Is this the type of workflow you're looking for, or are you trying to grab an attribute value by clicking it inside an infoWindow?
William,
So the question is, what is the simplest way to access a feature attribute and assign it to a variable without popups or info windows?
You would have to use a QueryTask and Query to get a feature unless you have access to the feature from a FeatureLayer selection or something. Or did I completely mis-interpert the question?
Robert,
I should be more specific. We are trying to have a feature on a map that the user can click on, after it is clicked the selected feature attribute is stored as a variable. This variable would then be used in a query to create an array of a Dojo chart(We got this part handled.)
Popup | ArcGIS API for JavaScript
For example, this popup example accesses attribute information, but the code doesn't show any click/selection code. I assume that there is a way to setFeatures, feature.attributes or access a feature index somehow. Not sure how.
William,
OK, under the hood of the API the the fact that the FeatureLayer has a infoTemplate defined it just does a Query/Selection based on the map click point to see if the map click hits a feature. So if you do not want to use popups when the feature is clicked then you just do the same thing.
I think I made some progress with this example: Query and edit related records | ArcGIS API for JavaScript
It has the on click event we were looking for "incidentLayer.on("click", function(evt) {" along with the relationship query I am looking for I think. This creates a popup with the query results instead of the graph that I want, which shouldnt be too hard to figure out.
I am going to try to piece this together and see if I have any luck.
William,
I'm not sure I completely understand the scenario either - and it looks like Robert also hit on this point - but you can set up a map click event to grab the feature's graphic, which contains its attributes, then send the desired attribute value to your query.
For example, something like this:
map.on("click", function(evt){ //if you click a graphic, grab the desired attribute if(evt.graphic){ var yourVariable = evt.graphic.attributes.FIELDNAME; } else{ return; } //send yourVariable to queryTask });
Is this the type of workflow you're looking for, or are you trying to grab an attribute value by clicking it inside an infoWindow?
This is exactly it.
Mathew, are you sure you still need a relationship?
It seems this queryTask might be easier to return your array.
Sorry for the delay, just getting back on this. So if I don't need the relationship class in the gdb, then would I have to load both feature services and just not display one?
I think you will have to load both feature services, regardless of whether or not you use a relate. It just depends which way you might want go-- relationship query or select a point and query with the selected point attribute.
I'm actually updating my app to use the select and query option for an actual map rather than my previous map-less ui. I'll let you know when I hit paydirt.