Accessing an attribute consumed by a popup in 4.xx API

255
2
Jump to solution
01-02-2020 07:28 AM
JohnAdams
New Contributor III

I am new to the 4.xx API and am trying to figure out how to access an attribute of a feature that's being consumed by my popup. I'd been using the 3.xx API for many years.

In the 3.xx API I did this all the time using IdentifyTask and IdentifyParameters with dojo.dom and dojo.on in my popups, with no problem.

However, with the 4.xx API, since the popup is now part of the MapView and you don't need all the Identify-things and dom and on to create a popup, I'm trying to figure out how to grab the value of one of the items in my popupTemplate.

I have a feature layer as thus:

var parcelsLayer = new FeatureLayer({
url: "https://jcgis.jacksongov.org/arcgis/rest/services/Cadastral/TaxParcelsTest/MapServer/0",
outFields: ["Parcel_ID","SitusAddress","SitusCity","owner"],
popupTemplate: thepopup
});‍‍‍‍‍‍‍

And before that I have my popup template:

var thepopup = {
title: "{SitusAddress}",
content: "<b>Parcel Num:</b> {Parcel_ID}<br><b>Address:</b> {SitusAddress}<br><b>City:</b> {SitusCity}<br><b>Owner:</b> {owner}"
}

So that when I click on a parcel, those 4 items are loaded into the popup. What I want to do is grab the "{Parcel_ID}" and assign its value to a variable, like:

         var parcelnum = grab the {Parcel_ID} somehow

I know that Popup has a selectedFeature property, which is a Graphic which has attributes, but it's not clear to me how to grab a particular attribute. As in something like:

         var parcelnum = view.popup.selectedFeature.attributes[1];

But of course that doesn't work that way.

Am I going about this the right way? Or if I want to do what I want to do, do I need to do it the same way I did using the 3.xx API?

Thanks.

0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there, 

You can watch the Popup's selectedFeature property to get access to attributes of clicked feature. See this test app how to do this. 

You can also set the content of your popupTemplate as a function. See this sdk documentWhen the feature is clicked, the feature is passed as an argument to the function and provides access to the feature’s graphic and attributes. See this test app how to use function for the content. 

Hope this helps,

-Undral

View solution in original post

0 Kudos
2 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

You can watch the Popup's selectedFeature property to get access to attributes of clicked feature. See this test app how to do this. 

You can also set the content of your popupTemplate as a function. See this sdk documentWhen the feature is clicked, the feature is passed as an argument to the function and provides access to the feature’s graphic and attributes. See this test app how to use function for the content. 

Hope this helps,

-Undral

View solution in original post

0 Kudos
JohnAdams
New Contributor III

Thanks, that was helpful. Setting up a watch function seems to work ... though it's tricky accessing my parcelnum variable outside of the watch function, which isn't ideal but I can probably work around that. My code is this:

var parcelnum;
view.popup.watch("selectedFeature", function()
{
if (view.popup.selectedFeature != null)
{
parcelnum = view.popup.selectedFeature.attributes.Parcel_ID;
alert("Parcel num is: " + parcelnum);
}
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos