Get Selected Layer

1114
6
Jump to solution
06-02-2014 08:20 AM
JillianStanford
Occasional Contributor III
Hi,
In my JS app I am overriding the default infoWindow and populating it with a custom dijit using the following code:

on(this.map.infoWindow, 'SelectionChange', lang.hitch(this, function() {                 var g = this.map.infoWindow.getSelectedFeature();                 if (g) {                 var options = { graphic: g };                 var cit = new ClipboardInfoTemplate(options, domConstruct.create("div"));                 cit.startup();                  this.map.infoWindow.setContent(cit.domNode);                 }             }));


This is working really well.

My question is, how can I determine which dynamic map service layer the selected graphic belongs to? I'd like to make some customizations to the dijit based on the layer and display the layer name, just like in the default popup but I can't seem to hook into the layer. Seems like I'm overlooking something obvious.

Thanks for the help!
Jill
0 Kudos
1 Solution

Accepted Solutions
JeffPace
MVP Alum
sorry i should have know that would be confusing.  Its from a much larger application

basically when you set the attributes on your graphic, you can add attributes. 

so if

attrs.layername="Layer";
var g = new esri.Graphic(f.geometry, sym, attrs);

and then you can access it in the popup. 

It would go right before you set the popup. Is it the result of a query? Seeing some of your code would be more helpful than me posting mine.

The code where you do the query and where you set the graphic/popup

View solution in original post

0 Kudos
6 Replies
JeffPace
MVP Alum
have you tried console.dir(g) to output the complete feature? My guess is there is an attribute in there that will provide you with the layername
0 Kudos
JillianStanford
Occasional Contributor III
have you tried console.dir(g) to output the complete feature? My guess is there is an attribute in there that will provide you with the layername


Hi Jeff,
Thanks for the suggestion!

Here is the console output (I replaced some of the actual data with placeholders):

{
   [functions]: ,
   __proto__: {
      [functions]: ,
      __proto__: { },
      _graphicsLayer: null,
      _shape: null,
      _visible: true,
      declaredClass: "esri.Graphic",
      visible: true
   },
   _graphicsLayer: null,
   _shape: null,
   _visible: true,
   attributes: {
      [functions]: ,
      __proto__: {
            [functions]: ,
            __proto__: null
      },
      datalink: "value",
      dateposted: "2/22/2012",
      Intersection: "y",
      intid: "Value",
      measure: "2.0869",
      OBJECTID: "2",
      other: "Null",
      postedby: "kdl",
      RID: "8",
      Road: "Value",
      road2: "Value",
      SHAPE: "Point",
      studydate: "2/9/2012"
   },
   declaredClass: "esri.Graphic",
   geometry: { },
   infoTemplate: {
      [functions]: ,
      -chains-: { },
      __proto__: { },
      _dateFormats: { },
      _fieldLabels: { },
      _fieldsMap: { },
      chartTheme: null,
      declaredClass: "esri.dijit.PopupTemplate",
      info: {
            [functions]: ,
            __proto__: { },
            description: "OBJECTID = 2<br/>SHAPE = Point<br/>Road = Value<br/>Intersection = y<br/>road2 = Value<br/>other = Null<br/>studydate = 2/9/2012<br/>datalink = Value<br/>dateposted = 2/22/2012<br/>postedby = kdl<br/>measure = 2.0869<br/>intid = Value<br/>RID = 8<br/>",
            title: "KDOT Traffic Counts Intersections"
      }
   },
   symbol: null,
   visible: true
}


I don't see a property that gets me the layer. There is the function, getTitle which returns the title of the infoWindow and is currently set to the layer name. That would technically work in this particular situation but doesn't seem like a robust solution since the title of the infoWindow could get changed elsewhere.

Thanks again for the help,
Jill
0 Kudos
JeffPace
MVP Alum
my recommendation would be to add the layer info to the attributes of the popup when you set it

var attrs = content;
//do some overrides
                        var contentreplacement = SearchParams.resultContentOverride(attrs.content, result.layers, f);
                        attrs.content = contentreplacement;
//add some links
                        attrs.links = links;
//override title
                        attrs.titleField = titleField;
                        //modify content only in result table
                        var fieldInfos = [];
                        array.forEach(fieldNames, function(name) {
                            fieldInfos.push({
                                fieldName: name, visible: true
                            });
                        });
                        //   alert(fieldInfos);
                        var g = new esri.Graphic(f.geometry, sym, attrs);
                        g.setInfoTemplate(new esri.dijit.PopupTemplate({
                            title: title,
                            fieldInfos: fieldInfos
                        }));
0 Kudos
JillianStanford
Occasional Contributor III
my recommendation would be to add the layer info to the attributes of the popup when you set it

var attrs = content;
//do some overrides
                        var contentreplacement = SearchParams.resultContentOverride(attrs.content, result.layers, f);
                        attrs.content = contentreplacement;
//add some links
                        attrs.links = links;
//override title
                        attrs.titleField = titleField;
                        //modify content only in result table
                        var fieldInfos = [];
                        array.forEach(fieldNames, function(name) {
                            fieldInfos.push({
                                fieldName: name, visible: true
                            });
                        });
                        //   alert(fieldInfos);
                        var g = new esri.Graphic(f.geometry, sym, attrs);
                        g.setInfoTemplate(new esri.dijit.PopupTemplate({
                            title: title,
                            fieldInfos: fieldInfos
                        }));


Hi Jeff,
I'm sorry, I don't really understand where this would go. I am setting the popup in the infoWindow's selection change event and just setting it's content to a dijit. I would like to add the layer info here but that's the crux of the issue, I can't get the layer info. What are the SearchParams and result.layers objects in the code above?

Thanks for walking me through this, I really appreciate the help,
Jill
0 Kudos
JeffPace
MVP Alum
sorry i should have know that would be confusing.  Its from a much larger application

basically when you set the attributes on your graphic, you can add attributes. 

so if

attrs.layername="Layer";
var g = new esri.Graphic(f.geometry, sym, attrs);

and then you can access it in the popup. 

It would go right before you set the popup. Is it the result of a query? Seeing some of your code would be more helpful than me posting mine.

The code where you do the query and where you set the graphic/popup
0 Kudos
JillianStanford
Occasional Contributor III
Thanks for the quick reply! Got it!
Many thanks for the help!
Jill
0 Kudos