Select to view content in your preferred language

Arcade Expression: Display only certain fields based on the value of another field

1545
5
05-16-2023 03:05 PM
Labels (2)
VanessaPocock
New Contributor III

Hi everyone,

I've been searching for possible help on this one, but can't find exactly what I need to figure out the arcade script for the following. 

All fields mentioned are in the same hosted layer. 

Based on the values in SHARING field (Public, Semi-Private, and Private) I need to restrict some or none of the other fields from the same layer from showing. 

Examples

- PUBLIC: all fields can show, no restrictions

- SEMI-PRIVATE: only Field1, Field2, Field3, Field4 can display in the pop-up

- Private: only Field1, and Field2 can display in the pop-up

Any help would be amazing!

Thank you

0 Kudos
5 Replies
jcarlson
MVP Esteemed Contributor

You can do this with an Arcade Popup Element, building the field list based on the sharing attribute.

// field objects to hold our popup contents; since even private has fields 1 and 2, we'll start with those
var fieldInfos = [
	{fieldName: 'field1'},
	{fieldName: 'field2'}
]

var attributes = {
	field1: $feature['field1'],
	field2: $feature['field2']
}

// get sharing attribute
var sharing = $feature['SHARING']

// if semi-private, add next set
if (sharing == 'SEMI-PRIVATE' || sharing == 'PUBLIC') {
	Push(fieldInfos, {fieldName: 'field3'})
	attributes['field3'] = $feature['field3']

	Push(fieldInfos, {fieldName: 'field4'})
	attributes['field4'] = $feature['field4']
}

// if public, add the rest

if (sharing == 'PUBLIC') {
	Push(fieldInfos, {fieldName: 'field5'})
	attributes['field5'] = $feature['field5']

	// and so on, for the rest of the attributes
}


return {
    type: 'fields',
    fieldInfos: fieldInfos,
    attributes : attributes
  }

 

- Josh Carlson
Kendall County GIS
VanessaPocock
New Contributor III

Hi Josh,

Thank you so much for the detailed reply. 

I was able to updated your code to include all of the fields I need. However, the pop-up content doesn't seem to actually change based on the "sharing" value. All records, regardless of their "sharing" value are displaying all 11 fields. 

Below is the code I modified. Any thoughts? 

// field objects to hold our popup contents; since even private has fields 1 and 2, we'll start with those

var fieldInfos = [
  {fieldName: 'field1'},
  {fieldName: 'field2'},
  {fieldName: 'field3'}
]

var attributes = {
  field1: $feature['HEADSTONE_ID'],
  field2: $feature['PLOT_ID_1'],
  field3: $feature['STATUS'],
  field4: $feature['FIRST_NAME'],
  field5: $feature['LAST_NAME'],
  field6: $feature['OTHER_NAMES'],
  field7: $feature['DATE_BIRTH'],
  field8: $feature['DATE_DEATH'],
  field9: $feature['AGE_AT_DEATH'],
  field10: $feature['PLACE_BIRTH'],
  field11: $feature['PLACE_DEATH']
}

// get sharing attribute
var sharing = $feature['SHARE']

// if semi-private, add next set
if (sharing == 'Semi-Private' || sharing == 'Public') {

  Push(fieldInfos, {fieldName: 'field4'})
  attributes['field4'] = $feature['field4']
 
  Push(fieldInfos, {fieldName: 'field5'})
  attributes['field5'] = $feature['field5']
 
  Push(fieldInfos, {fieldName: 'field6'})
  attributes['field6'] = $feature['field6']
}

// if public, add the rest

if (sharing == 'Public') {
  Push(fieldInfos, {fieldName: 'field7'})
  attributes['field7'] = $feature['field7']

  Push(fieldInfos, {fieldName: 'field8'})
  attributes['field8'] = $feature['field8']

  Push(fieldInfos, {fieldName: 'field9'})
  attributes['field9'] = $feature['field9']

  Push(fieldInfos, {fieldName: 'field10'})
  attributes['field10'] = $feature['field10']
 
  Push(fieldInfos, {fieldName: 'field11'})
  attributes['field11'] = $feature['field11']
}

return {
    type: 'fields',
    fieldInfos: fieldInfos,
    attributes : attributes
  }
 
Thank you,
Vanessa
0 Kudos
VanessaPocock
New Contributor III

Hi Josh,

I'm curious if you had a moment to look at my previous reply. 

Thank you,

Vanessa

0 Kudos
ErinCovill1
New Contributor II

Hi Vanessa, Just wondering if you got the expression to work? I am trying to do something similar.

Thanks!

Erin

0 Kudos
VanessaPocock
New Contributor III

Hi Erin,

No unfortunately I didn't. I tried the code provided by Josh, but something still wasn't working right. If you find a solution, please feel free to share!

Vanessa

0 Kudos