Use Selection From Select Widget in Web AppBuilder

2094
9
01-26-2018 12:33 PM
GrantTaylor2
New Contributor II

Hi,

Is it possible, and if so how do I use a selection set I've made with the Select Widget with another custom developed widget?

0 Kudos
9 Replies
GrantTaylor2
New Contributor II

I have the answer I was looking for. That will teach me to read the documentation properly in future ...

var count = 0;

array.forEach(this.map.itemInfo.itemData.operationalLayers, function (layer) {

  var featureLayer = layer.layerObject;

  count += featureLayer.getSelectedFeatures().length;

}, this);

0 Kudos
fahadrasheed
New Contributor III

Hi there i'm also facing this issue can you please elaborate the solution? 
i'm looking to get OIDs of features selected through select widget.
any sort of help / pointers will be appreciated.  

0 Kudos
GrantTaylor2
New Contributor II

Hi Fahad,

Get the name of the Id field by calling the objectidField method on a featureLayer.

Then if you call the getSelectedFeatures on a featureLayer you will get an array of Graphic objects.

Use the objectidField name to get the Id of the feature (Graphic) from its attributes array.

var ids = [];
array.forEach(this.map.itemInfo.itemData.operationalLayers, function (layer) {
   var featureLayer = layer.layerObject;
      array.forEach(featureLayer.getSelectedFeatures(), function (feature) {
         ids.push(feature.attributes[featureLayer.objectIdField]);
      }, this);
}, this);

I think that is the gist of it. Apologies my javascript is a bit rusty.

 

These may help:

Select with Feature Layer | ArcGIS API for JavaScript 3.34 

Graphic | API Reference | ArcGIS API for JavaScript 3.34 

FeatureLayer | API Reference | ArcGIS API for JavaScript 3.34 

0 Kudos
fahadrasheed
New Contributor III

Hi Grant,

Thank you for your kind reply. I have a simple widget in which a button in defined and on clicking the button i'm trying to define a function which will store OIDs of selected feature in a variable which i then pass on to some non esri webapps to preform some task. 
I have tried to apply your code in the function i defined for my button but it throws back an error "Uncaught reference error: array is no defined", as i'm also new in javascript i have no idea what i'm doing wrong here. I'll attach the picture of code that i have and it'll a great help if can take a look and can point out what is wrong with this code that i'm trying out. Code picture

0 Kudos
GrantTaylor2
New Contributor II

Hi Fahad,

You will need to add the array module to the define at the top of your code and also as a parameter to the function, see below.

define(['dojo/_base/declare',
        'jimu/BaseWidget',
        'dojo/_base/array',
        'esri/layers/FeatureLayer'
],
function(declare, BaseWidget, array) { ...‍‍‍‍‍‍‍‍‍‍‍‍

https://dojotoolkit.org/reference-guide/1.10/dojo/_base/array.html 

0 Kudos
fahadrasheed
New Contributor III

Hi Grant, 

Thanks again for your reply, I applied the changes as you guided in above comment but now it throw back a different sort of error "uncaught TypeError : array.forEach is not a function".
i read the dojo documentation about dojo/_base/array but i can not see what is wrong in this script. On removing array.forEach ids variable generate empty output.

 error2

0 Kudos
GrantTaylor2
New Contributor II

Hi Fahad,

Try putting the items in the function definition in the same order as they are in the declare array.

define(['dojo/_base/declare',
        'jimu/BaseWidget',
        'dojo/_base/array',
        'dojo/dom',
        'esri/map',
        'esri/layers/layer',
        'esri/graphic'
],
function(declare, BaseWidget, array) { ...
fahadrasheed
New Contributor III

Hi Grant, 

Finally it worked Thanks a lot.

One more question, what if the featurelayer i'm using in my web app has multiple featureclasses i.e. some different sort of point features and some line features, how can i limit the return of OID to only the feature which i want not others which may also get selected due to they overlay on each other.

Thank you Again.

0 Kudos
GrantTaylor2
New Contributor II

Hello Fahad,

You could try something like this...

var featureLayer = layer.layerObject;
if(featureLayer.geometryType === 'polyline') {
    ...
}

FeatureLayer | ArcGIS API for JavaScript 4.17 

0 Kudos