Select to view content in your preferred language

getSelectedFratures in ArcGIS js 4.x

739
1
12-21-2023 02:46 AM
majae
by
New Contributor

Hi,

I'm currently developing a widget using the ArcGIS API for JavaScript 4.x that will be integrated into a web application alongside various other widgets. However, I am facing challenges in how i should work with selection of features. In ArcGIS API for JavaScript 3.x, there was a convenient getSelectedFeatures method that provided all selected features from a layer, but I cannot find a direct equivalent in 4.x.

It seems that features in 4.x don't have a selected attribute, and after some research, it appears that the recommended approach is to use the Highlight method. However, I cannot find a method for getting all highlighted features, like getSelectedFeatures. Since the features might be selected from other widgets where my widget has no control, I'm looking for the standard way to select features and retrieve selected features. Any suggestions or advice on how I should work with selection in ArcGIS API for JavaScript 4.x would be greatly appreciated.

Thanks in advance!

0 Kudos
1 Reply
JoelBennett
MVP Regular Contributor

I don't think there's a documented way to do this. However, if you're willing to venture into the realm of the undocumented, you can easily get the list of the highlighted features' OBJECTID values from the layerView's "_highlightIds" property:

 

var objectIDs = Array.from(layerView._highlightIds.keys());

 

 

You can then use the layer's queryFeatures method to get the highlighted features:

 

var query = layer.createQuery();
query.objectIds = objectIDs;

layer.queryFeatures(query).then(function(featureSet) {
	//do something
});

 

 

Note that using undocumented features carries the risk of those features being modified or removed in a future release without notice.

0 Kudos