Select to view content in your preferred language

Use Arcade to add href hyperlinks to unique list

2719
18
Jump to solution
01-10-2024 10:33 AM
Paco
by
Frequent Contributor

Hello community!   I have a Field of unique coded values.  these unique codes reference a full URL in a Table. however for each Feature there could be multiple codes(that are separated by piping '|').  example: WoodOthers1948 | Barnes1953 | HailOthers1971

in the past,  I have hard coded the URLs to the codes and used HTML 'href' in the Arcade window to get my results,  however,  there are hundreds of these combinations.

I would like to have the attribute codes show in the popup and each individual code would be hyperlinked/href to their unique URL.

*this is visually how it should look in the popup,  but each coded value would be a unique hyperlink-

Data Source: WoodOthers1948 | Barnes1953 | HailOthers1971 | StevenOthers1974 | Condon1992 | CarrollOthers1999 | KirkhamNavarre2003 | CarrollOthers2014

I have thought of using 'Decode' to reassign the codes to URLs,  but not sure how to read the clicked $feature list in to assign those individual URLs each time.

Any help?  im assuming creating a unique Array will help, but accessing and href'ing each code correctly back to the coded text list is still an issue.

thx!

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Here's one way of doing it. I'm using a table I set up that has the codes and their associated URLs. In the Arcade element, I get the codes from the feature (here I use a dummy variable in lieu of an actual feature), split them into an array, and loop through that array. For each code, I get the URL from the table and create an href, which is pushed into an array. That array is concatentated with the Data Source text.

var feat = 'WoodOthers1948 | Barnes1953 | HailOthers1971';
var source = FeatureSetByPortalItem(myPortal, myTableId,  0, ['Code', 'URL'], false)
var arr = Split(feat, " | ")
var output = []
for (var index in arr) {
  var code = arr[index]
  var pub = Filter(source, "Code = @code")
  var item  = `<a href="${First(pub).URL}">${code}</a>`
  Push(output, item)
}

return { 
	type : 'text', 
	text : `<b>Data Source:</b> ${Concatenate(output, " | ")}`
}

This returns this popup, with each link functional

popup8.png

View solution in original post

18 Replies
KenBuja
MVP Esteemed Contributor

Here's one way of doing it. I'm using a table I set up that has the codes and their associated URLs. In the Arcade element, I get the codes from the feature (here I use a dummy variable in lieu of an actual feature), split them into an array, and loop through that array. For each code, I get the URL from the table and create an href, which is pushed into an array. That array is concatentated with the Data Source text.

var feat = 'WoodOthers1948 | Barnes1953 | HailOthers1971';
var source = FeatureSetByPortalItem(myPortal, myTableId,  0, ['Code', 'URL'], false)
var arr = Split(feat, " | ")
var output = []
for (var index in arr) {
  var code = arr[index]
  var pub = Filter(source, "Code = @code")
  var item  = `<a href="${First(pub).URL}">${code}</a>`
  Push(output, item)
}

return { 
	type : 'text', 
	text : `<b>Data Source:</b> ${Concatenate(output, " | ")}`
}

This returns this popup, with each link functional

popup8.png

Paco
by
Frequent Contributor

@KenBuja!   Thank you!   I just wasn't sure how to extract that array correctly,  this worked great.   I just replaced your dummy variable list with my polygon $feature layer, and used an existing Table that included the Codes and URLs, and the array was built off that.

Thanks for looking at this so quickly.  it definitely takes an extra second to think when it needs to sort thru a longer array list,  but it outputs what I need! 

here is an example of the new popup output, with active href links>

sources.png

0 Kudos
GeoFinn
Emerging Contributor

Hi Ken,

Thank you so much for sharing your example. 

I have run into something that is making me crazy! The code below modeled after your example is being used in a popup Arcade element. Its basically looping through a table of images and I’m reformatting the string into a usable URL. It seems to work when I run it in the code window, but it fails to render the links in the popup. I think it has something to do with the loop but the text that is output in the run looks correct.  

 

Any advice? (see images below)

 

 

 

var portal = Portal('https://gis.clark.wa.gov/portalpw/')

var Stmdocs = FeatureSetByPortalItem(portal,

    "4b35b095430d47d699dea875e62dd0b6", 3, [ 'MmsId','FilePath']) //42 is the table ID in the service

 

// Filter related features by using a common attribute

// Here both table have identical MMSID fields

var MMSID = $feature.MmsId

var filterStatement = 'MMSID = @MMSID'

 

// Related features as a variable

var relatedData = Filter(Stmdocs, filterStatement)

 

// Sort related features by oldest to newest

var relatedDataSorted = OrderBy(relatedData, 'MmsId')

// Build the pop-up string by iterating through all related features ideally as hyperlinks

var thenewpath = ''

var theurl = ''

var output = []

for (var f in relatedDataSorted){

    thenewpath = Replace(f.filepath, 'olympus', '')

    thenewpath = Replace(thenewpath, 'gisdata', '')

    thenewpath = Replace(thenewpath, 'Images', 'ccimages')

    thenewpath = Replace(thenewpath, '\\', '/')

    thenewpath = Replace(thenewpath, '////', 'https://gis.clark.wa.gov/')

    theurl = "<a href=" + TextFormatting.DoubleQuote + thenewpath + TextFormatting.DoubleQuote + ">Image</a>"

    Push(output, theurl)

    }

 

return {

  type : 'text',

  text : `<b>Image Links:</b> ${Concatenate(output, TextFormatting.NewLine)}`

}

GeoFinn_0-1722036093673.png

 

GeoFinn_1-1722036093677.png

 

0 Kudos
KenBuja
MVP Esteemed Contributor

That code should work. Did you check whether you're getting any valid related records? It would be something like this (untested)

var portal = Portal("https://gis.clark.wa.gov/portalpw/");
var Stmdocs = FeatureSetByPortalItem(portal, "4b35b095430d47d699dea875e62dd0b6", 3, ["MmsId", "FilePath"]); //42 is the table ID in the service

// Filter related features by using a common attribute
// Here both table have identical MMSID fields

var MMSID = $feature.MmsId;
var filterStatement = "MMSID = @MMSID";
// Related features as a variable
var relatedData = Filter(Stmdocs, filterStatement);
var output = 'No images found'
if (Count(relatedData) > 0) {
  // Sort related features by oldest to newest
  var relatedDataSorted = OrderBy(relatedData, "MmsId");
  // Build the pop-up string by iterating through all related features ideally as hyperlinks
  var thenewpath = "";
  var theurl = "";
  var list = [];
  for (var f in relatedDataSorted) {
    thenewpath = Replace(f.filepath, "olympus", "");
    thenewpath = Replace(thenewpath, "gisdata", "");
    thenewpath = Replace(thenewpath, "Images", "ccimages");
    thenewpath = Replace(thenewpath, "\\", "/");
    thenewpath = Replace(thenewpath, "////", "https://gis.clark.wa.gov/");
    theurl = `<a href="${thenewpath}">Image</a>`;

    Push(list, theurl);
  }
  output = `<b>Image Links:</b> ${Concatenate(list, TextFormatting.NewLine)}`;
}

return {
  type: "text",
  text: output
};

 

0 Kudos
Paco
by
Frequent Contributor
 

Hello again @KenBuja  and thanks again for the array to href code!

now I am building another JS(4.x) application using expressionInfos, and cannot seem to retrieve the related Table records to href those returns?   I can see the Feature array('dsource') just fine(image one) but I only get "object,FeatureSet" after trying to Filter the table('pub')..albeit the correct number of objects shows.

any help here?  the straight Arcade script you helped build works fine in a similar scenario using a MapView.  but this time I am accessing the Table using FeatureSetByPortalItem,  which shows the correct number of object rows so im assuming I am accessing correctly?

Im trying to relate the Table "DataSources_ID" with the Features "DataSourceID" and finally href the "URL" to the "DataSourcesID".

 

   expressionInfos: [{
    name: "SourceExpression",
    title: "Data Sources Title",
    expression: `
    var table = FeatureSetByPortalItem(Portal("https://portalname"), "d6260270918c477e9a76ee44ba0c145e", 1, ['DataSources_ID', 'URL'], false)
    var sources = $feature.DataSourceID
    var arr = Split(sources, "|")
    var output = []
   
     for (var i in arr) {
      var dsource = arr[i]
      var pub = Filter(table, "DataSources_ID = @dsource");
      //  var link  = <a href="{First(pub).URL}">{dsource}</a>
      push(output, pub)
    }

    return Concatenate(output,TextFormatting.NewLine)

    `
   }]

 

Paco_0-1733178979228.pngPaco_1-1733179029219.png

 

0 Kudos
KenBuja
MVP Esteemed Contributor

When posting code, please use the "Insert/Edit code sample" button. It makes it easier to read and refer to line numbers.

You're pushing a FeatureSet into the output array. You have to loop through the records in that FeatureSet and push each link into it.

for (var i in arr) {
  var dsource = arr[i]
  var pub = Filter(table, "DataSources_ID = @dsource");
  // var link  = <a href="{First(pub).URL}">{dsource}</a>
  // push(output, pub)
  for (var f in pub) {
    push(output, `<a href="${f.URL}">${dsource}</a>`
  }
}
0 Kudos
Paco
by
Frequent Contributor

Woops!  sorry about the pasting,  must have just kept pasting after i pasted the images.

thx again for the reply.  I understand your response,  but I think im having some formatting issues because I am getting JS errors in Visual Studio from the backticks in the Push statement?(image 1)

Ive had these before.   these errors do NOT show up when using the Arcade Expressions in AGOL,  but in my JS they seem to give me errors.  also the "$" i typically use seems to be read differently in standalone JS in certain places?

as you can see,  I use backticks in the beginning and end of my expression:, then I want to add the backticks again in the final href expression,  but the compiler does not like this.   

Paco_0-1733242935451.png

 

 

   expressionInfos: [{
    name: "SourceExpression",
    title: "Data Sources Title",
    expression: `
    var table = FeatureSetByPortalItem(Portal("https://portalurl"), "d6260270918c477e9a76ee44ba0c145e", 1, ['DataSources_ID', 'URL'], false)
    var sources = $feature.DataSourceID
    var arr = Split(sources, "|")
    var output = []

     for (var i in arr) {
      var dsource = arr[i]
      var pub = Filter(table, "DataSources_ID = dsource");
      //  var link  = <a href="{First(pub).URL}">{dsource}</a>
      //push(output, dsource)
      for (var f in pub) {
        push(output, `<a href="${f.URL}">${dsource}</a>`
      }
    }

    return Concatenate(output,TextFormatting.NewLine)

    `
   }]

 

0 Kudos
KenBuja
MVP Esteemed Contributor

My apologies, I left off the closing parenthesis

push(output, `<a href="${f.URL}">${dsource}</a>`);

 

0 Kudos
Paco
by
Frequent Contributor

I saw that and added the parens,  but that really wasn't the issue.   I must have some 'open' code somewhere,  because it just doesn't like the backticks within another backtick group.   the errors still persist.   

it shows " ',' expected" errors in VisualStudio and the console also shows "Uncaught SyntaxError: Unexpected identifier 'href' ".

so it's struggling with those backticks.   I really believe this would all work if i can figure out why that syntax is giving me errors.

also, if I remove the backticks from the start/end of the "expression:"  it can't read the first 'var' declaration. "Uncaught SyntaxError: Unexpected token 'var' "

 

thx!

0 Kudos