I have a problem that I can’t seem to get resolved.
I have a dataset that has an attribute with multiple entries separated by commas. I want the dashboard list to turn this into an array that lists these entries, and then include the title and the pdf link to these resources.
Example in the data
ID_num | Title | Related_nums | Pdf_link |
1 | Ocean | 3, 4 | Link 1 |
2 | Mountain | 3, 4, 5 | Link 2 |
3 | Lake | 2, 4, 5 | Link 3 |
4 | Stream | 1, 2, 3, 5 | Link 4 |
5 | Prairie | 4 | Link 5 |
To get the array, I used
var spacedRelNums = Replace($datapoint.Related_nums, ',', '<br>')
return {
attributes: {
Related_nums: spacedRelNums,
}
}
This is what I have, but I really would like the script to loop back around and put the names and links to the array table. I’m not sure how to do this.
I have been exploring using a data expression to create a dictionary to pull from, but then I don’t know how to reference that in the list widget since you can only have one data source.
Any help would be appreciated!
You won't be able to do it without getting into a Data Expression. The Advanced Formatting profile in Arcade can't see beyond the individual datapoint, so there's no way to grab attributes from another place.
var fs = FeatureSetByPortalItem(
Portal('your portal url'),
'itemid of service',
0, // or other layer index
['ID_num', 'Title', 'Related_nums', 'Pdf_link'],
false
)
// get the links as own dict in ID:Link format
var link_dict = {}
for (var f in fs) {
link_dict[f['ID_num']] = f['Pdf_link']
}
// output dict taking incoming schema plus a new field for links
var out_dict = {
fields: Splice(Schema(fs)['fields'], {name:'rel_links', type: 'esriFieldTypeString'}),
geometryType: '',
features: []
)
// loop through featureset
for (var f in fs) {
// get array of related numbers
var rel_num = Split(f['Related_nums'], ', ')
// loop through and build related link dict
var rel_link_dict = []
for (var n in rel_num) {
rel_link_dict[`link ${rel_num[n]}`] = link_dict[rel_num[n]]
}
// push attributes into out dict
Push(
out_dict['features'],
{ attributes: {
ID_num: f['ID_num'],
Title: f['Title'],
Related_nums: f['Related_nums'],
Pdf_link: f['Pdf_link'],
rel_links: Text(rel_link_dict)
}}
)
}
return FeatureSet(Text(out_dict))
There is admittedly a lot going on here, and I don't have similar data handy for testing, but I'll explain some key points.
While we could just spit out the links as a single text field, you'll run into a problem when you try to turn those into links. No matter what, you will need to use Advanced Formatting to turn the rel_links field into legitimate HTML in your list item.
Even if we had a list of "link 1, link 2" and used split to break it apart, how would we know which link was which anymore?
By loading the entire dict into a text field, you'll end up with a big string, something like:
{"link 1": "link from feature 1", "link 4": "link from feature 4"}
And we can pass that into FromJSON in the Advanced Formatting section to work with it as a dict again. It's a great way of passing variable-length lists of features / attributes from a Data Expression into the Advanced Formatting section. In your advanced formatting expression, you could try something like this:
var rel_links = FromJSON($datapoint['rel_links'])
var link_arr = []
for (var l in rel_links) {
Push(
link_arr,
`<a href="${rel_link[l]}">${l}</a>`
)
}
var link_text = Concatenate(link_arr, '<br>')
return {
attributes: {
link_text
}
}
Maybe that's more complicated than you were hoping for. 😬
But I do think it could work!
Thanks Josh! I will digest your solution. Many thanks!