Keyed Lists and Formmenu

Is it possible to use a keyed list in a formmenu with multiple set to yes? What I'm looking to do is have the form menu display options but return an abbreviation of the selected options. For example, in HTML, a select menu can display the states by their full name but return the two letter postal codes for the states.

Hey Bob,

Yes this is possible -- good timing, I was literally just working on something like this. It's a little circuitous to do this, but this is how I approached it. Side note: this is a great use for Data Blaze, I will add a Data Blaze example to this post a little later today.

{note}{formmenu: name=statepicker; values={=statenames}; multiple=yes}{endnote}{=join(stateabbrevs,", ")}

{statelist=[["full name":"Washington","abbreviation":"WA"],["full name":"California","abbreviation":"CA"],["full name":"Massachusetts","abbreviation":"MA"]]}
{statenames=[x["full name"] for x in statelist]}
{stateabbrevs=[x["abbreviation"] for x in statelist if contains(statepicker, x["full name"])]}

I only have 3 states in my keyed list called "statelist". That list contains both the full name of the state as well as the abbreviation. It's a list of lists, where each item in the "statelist" is a list of both the full name and abbreviation.

I then use list comprehension to create just a list of state names for the variable "statenames"

I use "statenames" as the values for my dropdown menu. I put the dropdown in a note so the selected values from the dropdown aren't inserted.

Then, I use list comprehension again to create stateabbrevs, which is a list of abbreviations being sourced from "statelist", but only if it's a selected value in the dropdown menu.

Finally, I display the "stateabbrevs" values with join( so that they're comma-separated.

This is a bit complex so let me know if you need any clarification or tips.

1 Like

Hi Andrew,

This is awesome. It's exactly what I wanted. As you said, it's a bit circuitous, but still manageable. Thanks so much!

For the Text Blaze folks, this would be so much easier if it were possible to use a keyed list for the values in the formmenu.

@Bob_Charpentier here's an example of how it's way easier in Data Blaze. Data Blaze snippets won't work in the community so I made a gif of it (see below). In this case, I just have a Data Blaze table with both the state and the state abbreviation. I can use the data blaze select menu to choose the options that I want, and tell it to only output the abbreviations. This is exactly what my previous snippet accomplishes, but without avoiding all the fancy dissecting and matching of a keyed list.

state abbreviations

1 Like

@Andrew_Hall Wow! That's a whole lot easier. Thanks for turning me on to Data Blaze.