Hi toddlers, Assuming I have a list of 10 items which are repeated from my Supabase DB. In my UI I just want one of the 10 items being displayed, ideally randomly. I know in Weweb and Webflow you can limit and filter the repeated collections but I havent found a solution. Thank you:)
Andreas Møller
1 year ago
You can use something like this formula
It generates a random number between 0 and the size of the array, and the extracts that item
Andreas Møller
1 year ago
If the list is small you could also do this. It sorts the array randomly and then gets the first item.
The second option is simpler, but less performant, so it should not be used if you have a very large array
PeterShaw
1 year ago
Thank you Andreas 🙂 When I am correct this is no longer a repeated list but youve created a variable containing the index of the items?
Andreas Møller
1 year ago
Yes, if you are only getting one element, you dont really need a repeated list, so you can just use a formula to get the random element. Just keep in mind that when a re-render occurs you will get a new element
If you only want to get the random element once, you should use a variable for holding the element, and then set it after the API response comes back. Does that make sense?
Thats what I had in mind. Add a button which will trigger rerendering so that a new item is displayed. I am stuck with getting the content from an API into a variable. It doesnt let me choose my API
Andreas Møller
1 year ago
Are you setting the initial value of the formula? That wont work, because at that time the API would not have been called yet
You have to use the success workflow in the api
PeterShaw
1 year ago
Great, that works. I added the formular and lastly I want to show the item as an output but it just says [object]. Do I have to add the formula to the text element itself or to the parent, too (as for repeated items). Thanks 🙂
Andreas Møller
1 year ago
You can use the Encode json formula is you want to print the data