Split and Get value with arcade

6522
38
Jump to solution
06-11-2021 08:06 AM
KARIMLABIDI
Occasional Contributor

Hello,

On Arcgis Portal, I would like to represent my buses line with Arcade and HTML. I would like get the number of the line and put colors in relation to the numbers.

My value are like that on my stop feature : Line 1, Line 2, ..., Test 56.

My first problem is to get the number only. I can split the field but then I don't know how the get the value.

Is someone think it's possible or too complicated? With IIf maybe?

I know this blog but my problem is little more different. https://www.esri.com/arcgis-blog/products/arcgis-online/mapping/combining-arcade-and-html-for-a-real...


Thank you

0 Kudos
38 Replies
John_Shell
New Contributor III

One of my expressions for the URL links coming from a field in my table.

// Write a script to return a value to show in the pop-up.
// For example, get the average of 4 fields:
// Average($feature.SalesQ1, $feature.SalesQ2, $feature.SalesQ3, $feature.SalesQ4)

var areas =FeatureSetByName($map,"Neighborhood Council Districts (Tacoma)");
var Onefifty = Buffer($Feature, -150, 'feet')
var countp = Intersects(areas,Onefifty);

var result = "";
for (var item in countp){
var id = item["URL"];
result += id



}
return result

0 Kudos
John_Shell
New Contributor III

Hi Pedro,

The issue is at the bottom of the pop-up when it lists out the names of the intersecting areas and the hyperlinks do not work correctly.

Link to WebViewer: https://tacoma.maps.arcgis.com/apps/mapviewer/index.html?webmap=c80eda9c8e434844a8ddbe742fc11a9c

Pop Up below

John_DShell_0-1662582344094.png

 

 

0 Kudos
PSGeo1
by
New Contributor III

Hi,

many thanks, for the support.

cheers

Pedro

The attributes as they are written field: 

Car,

or 

Boat

or 

Car, Boat

 

The code is this:


HTML:

 

<div style="display: {expression/expr4};"><a href="https://www.quicksilver-boats.com/media/391646/755-sundeck-running-268_original_hero_2000x1125.jpg?anchor=center&mode=crop&width=616&height=347&rnd=132161251460000000">{Equipment}</a></div>


<div style="display: {expression/expr3};"><a href="https://www.esmadrid.com/sites/default/files/styles/content_type_full/public/editorial/coches_compartidos_madrid.jpg?itok=gTSAwa66">{Equipment}</a></div>

 

HTML - but table incorporated.

 

<table style="width:100%;">
<tbody>
<tr style="display:{expression/expr4}">  <td style="width:200px; text-align:left; background-color:#302f2f; padding:5px"><a href="https://www.quicksilver-boats.com/media/391646/755-sundeck-running-268_original_hero_2000x1125.jpg?anchor=center&mode=crop&width=616&height=347&rnd=132161251460000000">{Equipment}</a> </td>  </tr>

<tr style="display:{expression/expr3}">  <td style="width:200px; text-align:left; background-color:#302f2f; padding:5px"><a href=" https://www.esmadrid.com/sites/default/files/styles/content_type_full/public/editorial/coches_compartidos_madrid.jpg?itok=gTSAwa66 ">{Equipment}</a> </td>  </tr>
</tbody></table>

 

 

Arcade:

Expression/expr3

//show_car

var arrName = Split($feature.Equipment, ",")

for (var i in arrName)    {

    if(arrName[i]=="car") {return "block"} 

    }

return "none"

 

Expression/expr4

/show_boat

var arrName = Split($feature.Equipment, ",")

for (var i in arrName)    {

    if(arrName[i]=="boat") {return "block"}

    }

return "none"

JohannesLindner
MVP Frequent Contributor

You use the Equipment field as link text. That means that you get the whole content of that field as link text, so if you have multiple equipment types, they will all be there.

Do it like this:

<table style="width:100%;">
<tbody>
<tr style="display:{expression/expr4}">  <td style="..."><a href="...">Boat</a> </td>  </tr>

<tr style="display:{expression/expr3}">  <td style="..."><a href="...">Car</a> </td>  </tr>
</tbody></table>

Have a great day!
Johannes
PSGeo1
by
New Contributor III

Hi,

just tested, In the morning I was quite busy with other stuff.

Works well for a single entry in the field. but if both are in the field (Car, Boat) …only the first one appears.

So I tried to change in the entry field from the "Car, Boat" to "Car,Boat" (no space after the comma) and guess what. it works.

My opinion is that such "space" shouldn't trigger a wrong answer to the call... it seems like a bug.
However, I just need to remove all spaces from the field for the final display to work. So I am ok with this.

Many thanks once more!

cheers, Pedro

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Ah, so your field formatting isn't consistent.

It isn't a bug, it's exactly what you told the computer to do. Take a look at your expressions:

Split($feature.Equipment, ",")

// "Car,Boat" -> ["Car", "Boat"]
// "Car, Boat" -> ["Car", " Boat"]
// -> Test for "Boat" will fail in the second case

Yeah, removing the spaces in the table would be good. Alternatively (or additionally), you can also remove the spaces in the expression:

Split(Replace($feature.Equipment, " ", ""), ",")

 


Have a great day!
Johannes
PSGeo1
by
New Contributor III

Well, thanks I'll keep this code in mind... but eventually I deleted all the spaces...

I still have two questions. one related with the header of the table that u just helped me. the fields can have single or multiple entries or are empty. and I am struggling with the empty part. I want the title to disappear when the field is empty.

This is the attempt to have a header, however it goes crazy :

<tr style="display:{expression/expr8}"><td style="padding:5px;background-color:{expression/expr12}><b>The following equipment is stored in the warehouse</b>>/div>

<table style="width:100%;">

<tbody>

 

the Arcade part:

expr8

iif(isempty($feature.equipment), "none", "block");

expr12

Var CD =$feature.region

when (cd=="Lisbon", "#ed5151",

cd=="Algarve","#149ece",

cd=="Alentejo", "#A7c636",

cd=="Centro", "#9E559C",

cd=="Norte", "#fc921f",

)

0 Kudos
JohannesLindner
MVP Frequent Contributor

Yeah, no wonder it goes crazy. Your HTML is pure chaos. It doesn't close the style declaration of the td, it doesn't close the td or the tr, it tries to close a div (???) but uses the wrong sign. Also, you have the tr outside of the table...

It's a good idea to write each tag on its own line, with indentation, and to close a tag before you work on its content. That way, you catch things like that more easily and the HTML becomes more readable. You can always shorten the code after making sure it works.

<table style="width:100%;">
  <tbody>
    <tr style="display:{expression/expr8};">
      <td style="padding:5px;background-color:{expression/expr12};">
        <b>The following equipment is stored in the warehouse</b>
      </td>
    </tr>
    <!-- other tr's -->
  </tbody>
</table>

 

Also, make sure that you actually have a default value in the When function in expr12.
https://developers.arcgis.com/arcade/function-reference/logical_functions/#when 


Have a great day!
Johannes
PSGeo1
by
New Contributor III

yes.

I am just trying to do the popup and I never used html before. 🙂
Thanks for the tip, I will try to be more organized.

cheers,

Pedro

 

 

0 Kudos