JS API version 4.12
I'm generating my own custom popup content, specific to each sublayer in my map image layer. I don't want to hard code dozens of fields in 40+ layers like this:
content: "<tr><td>Utility ID: </td><td>{UtilityID}</td></tr>"
It's going to be more of a custom template with some functions applied to common fields throughout many layers.
When creating a function for the sublayer.popupTemplate's content, the only thing I have available in the graphic's attributes are field names. Can I not easily get the aliases for these features? I don't see a more direct way to do this aside from a traversing through the metadata of the entire map image layer using an esriRequest...get layer field name/alias pairs into an array. Can I avoid the overhead of an esriRequest?
// Create identify task for the specified map service
var identifyTask = new IdentifyTask(app.mapImgLyrUtil.url);
// Set the parameters for the Identify
var params = new IdentifyParameters();
params.tolerance = 10;
params.layerOption = "visible";
params.width = app.mapView.width;
params.height = app.mapView.height;
const arrExcludeFields = ["OBJECTID", "SHAPE", "SHAPE.STLENGTH()"];
function executeIdentifyTask(event) {
params.geometry = event.mapPoint;
params.mapExtent = app.mapView.extent;
identifyTask
.execute(params)
}
app.mapView.on("click", executeIdentifyTask);
app.mapView.popup = {
dockEnabled: true,
collapseEnabled: false,
overwriteActions: true,
defaultPopupTemplateEnabled: true,
dockOptions: {
position: "bottom-right"
}
};
function createContent(content) {
var graphic = content.graphic; //graphic's attributes are FIELDNAME: {value}
var displayContent = "";
for (var key in graphic.attributes) { //key is the field name
if (arrExcludeFields.indexOf(key.toUpperCase()) == -1) {
var attrVal = "";
if (graphic.attributes[key]) {
attrVal = graphic.attributes[key];
}
displayContent += key + ": " + attrVal + "<br>";
}
}
return displayContent;
}
app.mapImgLyrUtil.allSublayers.map(function (sublayer) {
sublayer.popupTemplate = new PopupTemplate({
outFields: ["*"],
content: createContent,
title: sublayer.name
});
return sublayer;
});
Solved! Go to Solution.
Ben, I don't know of a way to use Arcade in a generic sense to retrieve field aliases. Can I avoid having to hard code every single field name ($feature.UtilityID, $feature.ProjectName, $feature.InstalledDate, etc...)? Hard coding expressions with specific field names for dozens of layers is out of the question.
I was just looking for a more efficient way to translate field names to field aliases while establishing my popup templates for each of the layers.
Here's my current solution, as mentioned above, using a request against the service and building a dictionary.
var arrLayerFields = {};
esriRequest(app.mapImgLyrUtil.url + "/layers?f=json").then(function (response) {
var layerInfos = response.data.layers;
layerInfos.forEach(function (lyr) {
if (lyr.type == "Feature Layer") {
var arrFieldInfos = lyr.fields;
arrFieldInfos.forEach(function (fieldInfo) {
delete fieldInfo.type; // \
delete fieldInfo.length; // } Don't need these field properties
delete fieldInfo.domain; // /
});
arrLayerFields[lyr.name] = arrFieldInfos;
}
});
});
//creates an object of layers with an array of fields (name and alias properties)
If I am understanding you correctly, look into Arcade: Arcade | ArcGIS API for JavaScript 4.12
Here is a sample using arcade for popups: Reference Arcade expressions in PopupTemplate | ArcGIS API for JavaScript 4.12
Ben, I don't know of a way to use Arcade in a generic sense to retrieve field aliases. Can I avoid having to hard code every single field name ($feature.UtilityID, $feature.ProjectName, $feature.InstalledDate, etc...)? Hard coding expressions with specific field names for dozens of layers is out of the question.
I was just looking for a more efficient way to translate field names to field aliases while establishing my popup templates for each of the layers.
Here's my current solution, as mentioned above, using a request against the service and building a dictionary.
var arrLayerFields = {};
esriRequest(app.mapImgLyrUtil.url + "/layers?f=json").then(function (response) {
var layerInfos = response.data.layers;
layerInfos.forEach(function (lyr) {
if (lyr.type == "Feature Layer") {
var arrFieldInfos = lyr.fields;
arrFieldInfos.forEach(function (fieldInfo) {
delete fieldInfo.type; // \
delete fieldInfo.length; // } Don't need these field properties
delete fieldInfo.domain; // /
});
arrLayerFields[lyr.name] = arrFieldInfos;
}
});
});
//creates an object of layers with an array of fields (name and alias properties)