Can I populate a {formmenu} from a DataBlaze table?
I realise that I can get the same result with just a {dbselect } statement but am interested in the concept.
Hi Tony,
Yes, this is possible! Here's an example:
{note}{dbselect: SELECT Name FROM Table1; space=id; multiple=yes; menu=no; name=crm-names}{customers=`crm-names`}{endnote: trim=yes}
{formmenu: name=dropdown; values={=map(customers, c -> c["name"])}}
Thank you. Could you please explain, in simple terms, the syntax of the map function.
Sure! map() takes a list, and returns a new version where each element is replaced with the value you specify.
For example, in the snippet above, it took the list where the values are like ["name": "John"] and returned a version where it only outputs the value for 'name'.
Example syntax: map([1, 4, 9], (x) -> sqrt(x))
You can replace the brackets & text inside it with your list's name, then change how you want to output 'x', which is 'c' in my snippet above.
You can read more about list functions here:
Thanks. As a follow on, how can i reference Price in the snippet below. I need it to show after the final @ symbol
{note}{dbselect: SELECT Ref, Price FROM Range ORDER BY Ref ASC; space=1IHeI043ip48EwKEPxb3ie; multiple=yes; menu=no; name=range}{yarns=range}{endnote: trim=yes}
In {formmenu: name=dropdown; values={=map(yarns, c -> c["ref"])}} @
How is price formatted in your Data Blaze table and how would you like it to appear in the snippet? Do you want it as options in a drop-down menu as well?
Not as a dropdown. In DataBlaze the price is formatted as a number with 2 decimal places and a £ prefix. I need the Price relative to the Ref preferably in currency format.
Gotcha, thank you for confirming. I updated my example do what you are looking for:
{note}{dbselect: SELECT Name, Price FROM Table1; space=id; multiple=yes; menu=no; name=crm-names}{customers=`crm-names`}{endnote: trim=yes}
For {formmenu: name=dropdown; values={=map(customers, c -> c["name"])}} @ {=find(customers, c -> c["name"] = dropdown)["price"]; format=$,.2f}.
Thanks Dylan. Working well now.