Is there any way to grab attributes of a selected feature without using/listening to the popup?

236
2
Jump to solution
07-22-2021 03:28 PM
Jackson_CountyMissouri
New Contributor II

I have a feature layer and I'm trying to grab an attribute from a feature the user clicked on.

The standard way to do that would be to do something like:

myView.popup.watch("selectedFeature", function()
{
  if(myView.popup.selectedFeature != null)
  {
      var myVar = myView.popup.selectedFeature.attributes.Thing;
  }
}

The problem with doing that is that every time the popup pops up, it runs that code. For example, if you also have a search widget that searches for something and selects something in another layer, it thus has a popup, and then runs that code anyway simply because it has a popup. But of course since that search is dealing with a totally different layer, you don't want that code to be invoked. That is, it's running that listener for no reason at all.

What I would like to do is something like this:

myView.on("click", function ()
{
  var myVar = myView.selectedFeature.attributes.Thing;
}

This would let me deal with the selected feature totally independent of any popup. Unfortunately, as far as I can tell that's impossible because for some reason ESRI decided to make the selected feature an attribute of the popup instead of an attribute of the view or the feature layer (which would make a ton more sense, but I digress).

Anybody have any ideas?

0 Kudos
1 Solution

Accepted Solutions
JeffreyWilkerson
Occasional Contributor

I think you can do exactly what you are talking about using the 'View.on("click"' and the 'hittest' to get selected features, like this:

// Set up function to collect location if view is clicked 
view.on("click", (event) => {
    // Pull selected features if any
    view.hitTest(event).then((response) => {
        var results = response.results;
        if (results.length > 0) {
            for (var i = 0; i < results.length; i++) {
                 console.log(results[i]); 
            }
        }
    }
}

View solution in original post

2 Replies
JeffreyWilkerson
Occasional Contributor

I think you can do exactly what you are talking about using the 'View.on("click"' and the 'hittest' to get selected features, like this:

// Set up function to collect location if view is clicked 
view.on("click", (event) => {
    // Pull selected features if any
    view.hitTest(event).then((response) => {
        var results = response.results;
        if (results.length > 0) {
            for (var i = 0; i < results.length; i++) {
                 console.log(results[i]); 
            }
        }
    }
}

View solution in original post

Jackson_CountyMissouri
New Contributor II

Thanks, that works. Specifically I've got this:

 

	myView.on("click", (event) =>
	{
		myView.hitTest(event).then((response) =>
		{
			var result = response.results[0].graphic.attributes;
			var myVar = result.Thing;
		});
	});

 

0 Kudos