Ok, this time we're concluding on the paths opened last time in this post : DB-tables, One snippet to call them all #2 : Benihikō
Here is the final script we reached, las time: Copy of snippet "Benihikō 11, final form"
By making the whole process repeatable at will.
To do so, we'll simply wrap the previous script in a {repeat} command so that the option to fetch another (batch of) row(s) appears whenever you fill out the snippet form. Using the "locals" parameter so when can invoke the {repeat} variables outside the bounds of repeat command.
And some basic bits of code inside the {button} command to make the whole thing interactive.
Since the "Table" doesn't print in the Community post, I'll do a simpler version without table hear...and then put the link to the complete version (snippet exchange).
Last post was quite long so we'll go straight to final form:
{repeat: for x in seq(1,rep); locals=outputs}
{=catch(output, "Make sure your table ID and name is written in appropriate places and the snippet form is completed") }{note}
22Char ID of your DB table
{formtext: name=Table_id; default=22Char ID of your DB table; cols=22}
{urlload: https://data-api.blaze.today/api/database/fields/table/{=table_id}/; done=(res) -> ["Data":(res)]}
{Resulta=lower(fromjson({=Data}))}{liste=[n.name for n in resulta]}{=liste}
Final form.
The multiple options always take priority over the single choice mode. But the single choice mode is the default mode where the snippet start.
If you activated the multiple option (by selecting values in the respective {formmenu and/or {dbselect but want to revert back: simply empty your selection !
{mlist=["product_name", "color", "price", "origin", "lenght", "motto"]}mlist={=mlist}
{if: not {=Multi_Fields}}{formtoggle: name=Multi_Fields; default=no}{endformtoggle}
Single choice:
{formmenu: values={=mlist}; name=onefield; multiple=no}
{else}{formtoggle: name=Multi_Fields; default=no}
Multiple choices:
{formmenu: name=several_fields; multiple=yes; values={=mlist}}{endformtoggle}{endif}
{if: not {=Multi_lines}}{formtoggle: name=Multi_lines; default=no}{endformtoggle}
One record:
{dbselect: SELECT product_name, Color, Origin, Lenght, motto, Price FROM Mock_Data; space=id; menu=yes; name=OneRow; multiple=no}
{else}{formtoggle: name=Multi_lines; default=no}
Several record:
{dbselect: SELECT product_name, Color, Price, Origin, Lenght, motto FROM Mock_Data; space=id; menu=yes; name=MultiRowS; multiple=yes}{endformtoggle}{endif}
Fields={if: count(several_fields)>0}{Fields=several_fields}{else}{Fields={=[ onefield]}}{endif}{=fields}
Rows={if: count(multirows)>0}{Rows=multirows}{else}{Rows=[ onerow]}{endif}{=rows}
Unique output = {Output=[[r[f] for f in fields] for r in rows]}{=output}{endnote}
{endrepeat}
{formtext: name=rep; default=1}
{button: rep= rep - 1
; label=Reduce one!}{button: rep= rep + 1
; label=Add one!}
{=map(outputs, y -> y.output)}
And here is the link to the proper version in the snippet exchange (the table adds much more readability): Copy of snippet "Benihikō 13, repeating final form"
Of course, if you use this script, you might have to clean up as it amounts to quite a lot of variables and number of outputs.
So now with this setup we end up with an ordered list of ordered lists of ordered lists (3 levels)
Next time, we'll focus on the prettyfication of said output. Usingg mostly the join() function, the map() and filter(), the {repeat} command and maybe some [list comprehension].
While also starting to explore ways to print the keys/headers with the data we fetch.
Ciao!