Custom popup actions per feature

This device does not support 3D.

View the system requirements for more information.

It is possible to customize Popup and PopupTemplate actions per feature attribute.

This sample demonstrates how to format custom actions specifically per selected feature. It uses a Popup and a PopupTemplate. Although actions can be added via the actions property on either the Popup or PopupTemplate, the actions in this sample are specifically set within the featurelayer's popupTemplate.

 popupTemplate: {
   actions: [{
     id: "find-brewery",
     image: "beer.png",
     title: "Brewery Info"
   }]
 }

Actions are styled by either using the class or image property. If using class, you must use an icon font. If using image, you must provide a valid URL to an image that will be used as a background-image for the action. In this specific example, an image is used to indicate the action button.

How it works

The PopupViewModel's trigger-action is fired and then checks for the action ID. In this case, it's listening for an ID called 'find-brewery'.

popup.viewModel.on("trigger-action", function (event) {
  if (event.action.id === "find-brewery") {
    // do something
  }
});

Next, it grabs the attributes of the selected feature. We don't need all of them, so in this example we explicitly grab the attribute with the name "website" and store it in a new variable called info.

var attributes = popup.viewModel.selectedFeature.attributes;
// Get the "website" field attribute
var info = attributes.website;

After this, we check to make certain this value is not null. If not, some string manipulation is performed and the URL value stored within the "website" field is opened up in a new browser window. If the field value is null, a new browser window opens up using a Google search based off the attribute's "name" field.

{
  // Make sure the "website" attribute value is not null
  if (info !== null){
  // Open up a new browser using the URL value in the 'website' field
    window.open(info.trim());
    // If the "website" value is null, open a new window and Google search the name of the brewery
  } else {
    window.open("https://www.google.com/search?q=" + attributes.name);
  }
}

Sample search results

TitleSample

There were no match results from your search criteria.