Detecting selection event on Query or Attribute Table widgets

978
4
Jump to solution
11-10-2016 04:29 AM
ahuarte
New Contributor III

Hi, I'm trying to find a way to detect the event of feature selection of a ArcGISMapServiceLayer loaded in a map. Or get the features selected by user using the Query or Attribute Table widgets.

I don't know how to get the FeatureLayer collection of the ArcGISMapServiceLayer instance to attach their 'selection-change'/'selection-clear' events. I see the 'layersInfo' member successfully but the method 'map.getLayer(id)' always return null to these sublayer collection.

Is it possible? How I do it?

Thanks in advance

Alvaro

0 Kudos
1 Solution

Accepted Solutions
ahuarte
New Contributor III

Hi Robert, thanks for your review.

Finally I implemented the third option overriding the original methods where the widgets manage the query results. And It works fine!

My code for Query widget....

var widgetConfigs = null;

if ((widgetConfigs = widget.appConfig.getConfigElementsByName("Query")) != null && widgetConfigs.length > 0) {
  var connectFunction = null;
  var queryWidget = widget.widgetManager.getWidgetById(widgetConfigs[0].id);

  // ... override methods.
  connectFunction = function (currentWidget) {
    var theWidget = currentWidget;
    var theOriginalResultItemsFunction = theWidget._addResultItems;
    var theOriginalClearResultPageFunction = theWidget._clearResultPage;

    if (!theOriginalResultItemsFunction || !theOriginalClearResultPageFunction)
       return;

    theWidget._addResultItems = function (features, resultLayer, relatedResults, relatedTableIds) {
       var self = this;
       theOriginalResultItemsFunction.call(self, features, resultLayer, relatedResults, relatedTableIds);

       ... my bla bla
    };
    theWidget._clearResultPage = function () {
       var self = this;
       theOriginalClearResultPageFunction.call(self);

       ... my bla bla
    };
  };
  if (!queryWidget) {
    widget.widgetManager.loadWidget(widgetConfigs[0]).then(lang.hitch(widget, function (currentWidget) { connectFunction(currentWidget); }));
  }
  else {
    connectFunction(queryWidget);
  }
 }

🙂 Thanks Robert!

Alvaro

View solution in original post

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Alvaro,

   A ArcGISMapServiceLayer is just an image from the server and does not have geometries returned to the map. The query widget and the AT widget are able to select features because they use a QueryTask on the map service or create a FeatureLayer fro the query results. The query widget adds the QueryTask results as a FeatureLayer using a FeatureCollection.

0 Kudos
ahuarte
New Contributor III

Hi Robert, sorry I wrote a wrong name class, I have a ArcGISDynamicMapServiceLayer instance. Its layerinfos member contains the layers I want attach, but I looked that AT and Query widgets create their own "private" FeatureLayer and QueryTask instances.

I see four options:

  • Edit these widgets to emit 'selection' events using map as target, and I attach them in my own widget.
  • Edit these widgets to use the publishData/onReceiveData comunication.
  • Override some methods of these widgets in my own widget to avoid edit the original code adding my own emit events.
  • Override the FeatureLayer constructor in my widget to attach any instance created in any widget of the application and then to attach the 'selection-complete'/'selection-clear' events of FeatureLayers with a specific url.

First and second options force to me to deploy new "rewrited" widgets, I don't like it 😞

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Alvaro,

   I have looked into this a few minutes and all I can say is you have your work cut out for you. Option 1 and 2 are definitely the easy route initially but like you said maintainability. Good luck on this venture.

0 Kudos
ahuarte
New Contributor III

Hi Robert, thanks for your review.

Finally I implemented the third option overriding the original methods where the widgets manage the query results. And It works fine!

My code for Query widget....

var widgetConfigs = null;

if ((widgetConfigs = widget.appConfig.getConfigElementsByName("Query")) != null && widgetConfigs.length > 0) {
  var connectFunction = null;
  var queryWidget = widget.widgetManager.getWidgetById(widgetConfigs[0].id);

  // ... override methods.
  connectFunction = function (currentWidget) {
    var theWidget = currentWidget;
    var theOriginalResultItemsFunction = theWidget._addResultItems;
    var theOriginalClearResultPageFunction = theWidget._clearResultPage;

    if (!theOriginalResultItemsFunction || !theOriginalClearResultPageFunction)
       return;

    theWidget._addResultItems = function (features, resultLayer, relatedResults, relatedTableIds) {
       var self = this;
       theOriginalResultItemsFunction.call(self, features, resultLayer, relatedResults, relatedTableIds);

       ... my bla bla
    };
    theWidget._clearResultPage = function () {
       var self = this;
       theOriginalClearResultPageFunction.call(self);

       ... my bla bla
    };
  };
  if (!queryWidget) {
    widget.widgetManager.loadWidget(widgetConfigs[0]).then(lang.hitch(widget, function (currentWidget) { connectFunction(currentWidget); }));
  }
  else {
    connectFunction(queryWidget);
  }
 }

🙂 Thanks Robert!

Alvaro

0 Kudos