Select to view content in your preferred language

Display field alias and published field order in pop up via Arcade

210
1
08-08-2024 12:27 PM
Labels (1)
Brian_McLeer
Frequent Contributor

In migrating applications from WAB to Ex Builder, I am configuring pop ups via Arcade. The idea of the code below is to not display the fields in "fieldsToExclude", then display all other fields in every  layer for the webmap using the same code and hyperlink when applicable. The issue I have been having is I cannot find a way to have the pop ups display the field alias, where it is only displaying the field name. The second issue is that the pop up is not displaying the fields in order of how the service is published. 

 

 

// General and URL Fields Formatting
var fieldsToExclude = ["OBJECTID", "SHAPE", "LAYERNAME", "GLOBALID", "SHAPE.STAREA()", "SHAPE.STLENGTH()", "CREATED_USER", "CREATED_DATE", "LAST_EDITED_USER", "LAST_EDITED_DATE", "LASTMODBY", "LASTMODDATE", "LASTSYNDATE"];
var content = "";
var seenUrls = {};

// Function to format URL fields
function formatURL(label, value) {
    return "<b>" + label + ":</b> <a target='_blank' href='" + value + "'>Click here for more info.</a><br/>";
}

// Iterate over each field in the $feature
for (var field in $feature) {
    var value = $feature[field];
    var alias = field; // Use the field name as a fallback alias

    // Exclude certain fields and check if value is not empty
    if (IndexOf(fieldsToExclude, Upper(field)) == -1 && !IsEmpty(value) && Upper(value) != "NULL") {
        // Check if the field value is a URL and format accordingly
        if (Upper(Left(value, 7)) == "HTTP://" || Upper(Left(value, 8)) == "HTTPS://") {
            if (!HasKey(seenUrls, value)) {
                content += formatURL(alias, value);
                seenUrls[value] = true;
            }
        } else {
            content += "<b>" + alias + ":</b> " + Text(value) + "<br/>";
        }
    }
}

return {
    type: 'text',
    text: content // this property supports HTML tags
};

 

 

Brian
0 Kudos
1 Reply
Brian_McLeer
Frequent Contributor

After seeing some related articles below after I posted this question,  it resolved showing the field alias instead of the field name. Below is the updated code. I am still trying to have it show the fields in published order. 

https://community.esri.com/t5/arcgis-online-questions/issue-with-arcade-expression-in-popup-fields-o...

https://developers.arcgis.com/arcade/function-reference/feature_functions/#expects

 

 

var fieldsToExclude = ["OBJECTID", "SHAPE", "LAYERNAME", "GLOBALID", "SHAPE.STAREA()", "SHAPE.STLENGTH()", "CREATED_USER", "CREATED_DATE", "LAST_EDITED_USER", "LAST_EDITED_DATE", "LASTMODBY", "LASTMODDATE", "LASTSYNDATE"];
var content = "";
var seenUrls = {};

// Function to format URL fields
function formatURL(label, value) {
    return "<b>" + label + ":</b> <a target='_blank' href='" + value + "'>Click here for more info.</a><br/>";
}

// Ensure all fields are available
Expects($feature, "*");

// Get the schema of the feature's layer
var schemaDict = Schema($feature);
var fieldsArray = schemaDict["fields"];

// Function to get the alias of a field
function getFieldAlias(fieldName) {
    for (var j = 0; j < Count(fieldsArray); j++) {
        var fieldDict = fieldsArray[j];
        if (fieldDict['name'] == fieldName) {
            return fieldDict['alias'];
        }
    }
    return fieldName; // Fallback to field name if alias is not found
}

// Iterate over each field in the schema to respect the field order
for (var i = 0; i < Count(fieldsArray); i++) {
    var fieldDict = fieldsArray[i];
    var fieldName = fieldDict["name"];
    var alias = fieldDict["alias"];
    var value = $feature[fieldName];

    // Exclude certain fields and check if value is not empty
    if (IndexOf(fieldsToExclude, Upper(fieldName)) == -1 && !IsEmpty(value) && Upper(value) != "NULL") {
        // Check if the field value is a URL and format accordingly
        if (Upper(Left(Text(value), 7)) == "HTTP://" || Upper(Left(Text(value), 8)) == "HTTPS://") {
            if (!HasKey(seenUrls, value)) {
                content += formatURL(alias, value);
                seenUrls[value] = true;
            }
        } else {
            content += "<b>" + alias + ":</b> " + Text(value) + "<br/>";
        }
    }
}

return {
    type: 'text',
    text: content // this property supports HTML tags
};

 

 

Brian
0 Kudos