Select to view content in your preferred language

Passing Attribute Value to Variable

1913
10
Jump to solution
05-01-2014 07:22 AM
LeviRoberts
Regular Contributor
We are trying to set up a simple application that only uses one layer. This layer is a grid for a mapbook. We want users to be able to click one of the grid features and it open a pdf on a separate window. The only thing I'm hung up on currently is trying to grab an attribute value and pass it to a variable. One of the attributes for the feature is the URL to the PDF.

var mapBook = new FeatureLayer("[Webservice]", {  mode: FeatureLayer.MODE_SNAPSHOT  });  map.addLayer(mapBook);  mapBook.on("click", function(evt) {  var url = [Need to pass attribute value here];         window.open(url);  });


Do I need to use Query or Identify task? Or is there something else?
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Steve,

You could use the following:

mapBook.on("click", function(evt) {           var url = evt.graphic.attributes.<field name>;           window.open(url); });


Simply replace <field name> with the field name that contains the URL.

View solution in original post

0 Kudos
10 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Steve,

You could use the following:

mapBook.on("click", function(evt) {           var url = evt.graphic.attributes.<field name>;           window.open(url); });


Simply replace <field name> with the field name that contains the URL.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
One other thing, you will need to specify the outFields parameter for the Feature Layer:

var mapBook = new FeatureLayer("[Webservice]", {
    mode: FeatureLayer.MODE_SNAPSHOT,
    outFields: ["*"]
});
0 Kudos
LeviRoberts
Regular Contributor
Perfect. Works like a charm now. Thanks for your help.
0 Kudos
MichaelVolz
Esteemed Contributor
Steve:

Have you been able to get your sample to work with Jake's comments?

I tried the sample with other additional required items and it does not work.

Could you post all the code for your sample?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Steve,

Could you mark this thread as 'resolved/answered' to help other users in the community?

Thanks!
0 Kudos
MichaelVolz
Esteemed Contributor
Here is my sample that hopefully someone can see where I am going wrong as I am getting errors and unable to get the attribute from the Feature Layer - I cannot get the app to show the URL value in the line console.log("url =  ", url); below:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>FeatureLayer On Demand</title>

    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">
    <style>
      html, body, #mapDiv {
        padding:0;
        margin:0;
        height:100%;
      }
    </style>
    <script src="http://js.arcgis.com/3.9/"></script>
    <script>
      var map;
      require([
        "esri/map", "esri/layers/FeatureLayer",
        "dojo/parser", "dojo/domReady!"
      ], function(
        Map, FeatureLayer,
        parser
      ) {
        parser.parse();
        map = new Map("mapDiv", {
          basemap: "national-geographic",
          center: [-75.56, 40.0025],
          zoom: 10
        });
      
  var parcels = new FeatureLayer("Feature Layer REST endpoint URL", {
   mode: FeatureLayer.MODE_SNAPSHOT,
   outFields: ["*"]
  });
  console.log("hello");
  map.addLayer(parcels);
   
  parcels.on("click", function(evt) {
  var url = evt.graphic.attributes.UPI;
   console.log("url =  ", url);
    });
 
      });
    </script>
  </head>
  <body class="claro">
    <div id="mapDiv">
    </div>
  </body>
</html>

Can anyone see what I am doing wrong?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Mike,

Your code looks correct.  Be sure the field you are referencing is 'UPI' and not 'upi'.  Here is an example of your code with a sample service:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples
    on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>FeatureLayer On Demand</title>

    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/dojo/dijit/themes/claro/claro.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">
    <style>
      html, body, #mapDiv {
        padding: 0;
        margin: 0;
        height: 100%;
      }
    </style>
    <script src="http://js.arcgis.com/3.9/"></script>
    <script>
      var map;
      require(["esri/map", "esri/layers/FeatureLayer", "dojo/parser", "dojo/domReady!"], function(Map, FeatureLayer, parser) {
        parser.parse();
        map = new Map("mapDiv", {
          basemap : "national-geographic",
          center : [-81.792107, 26.150807],
          zoom : 9
        });

        var parcels = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0", {
          mode : FeatureLayer.MODE_SNAPSHOT,
          outFields : ["*"]
        });

        map.addLayer(parcels);

        parcels.on("click", function(evt) {
          var url = evt.graphic.attributes.areaname;
          console.log("url = ", url);
        });

      });
    </script>
  </head>
  <body class="claro">
    <div id="mapDiv"></div>
  </body>
</html>
0 Kudos
KenBuja
MVP Esteemed Contributor
Check the capabilities of your map service. I noticed that when using a service like Jake did, it works fine. However, if I used a service from an older server (http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3, for example), the click event would never get activated.
0 Kudos
MichaelVolz
Esteemed Contributor
Jake:

First off thanks for posting my modified sample with your layers.

I have some follow-up questions.

1.) Do you have to use a FeatureLayer to achieve this functionality? 

2.) Can you call the data another way as there is by default a 1000 feature limit (This can be increased but it takes a toll on application performance)?

I ask this because I have a feature layer with over 100,000 features and I only saw 1 feature in the map extent I was using which I'm now thinking was because this was the only feature visible in the first 1000 records of the 100,000 record feature layer.
0 Kudos