This is an interesting question/topic. I’m surprised it wasn’t answered before (I think I missed it). Unfortunately there isn’t much documentation about using arrays and storage options in Wappler.
Before the array component was added, I used cookies to create a wish list. I added a buttons to add/remove the item id to/from the cookie. To retrieve the items from the database, the cookie value can be used in the query. This method works alright, but it’s fiddly managing the cookies and the storage is quite limited. I think I would probably use the array component for this now. I’m not sure of the best way to do this, but this is what I just tried:
I created a local storage array - ‘idlist’ - because I want the selection to persist (like a cookie).
I added an array component to which I added a dynamic event:
I added two buttons, to add/remove items to/from the array:
arr_selectedItems.addUniq(stock_id) (‘remove’ for the other button)
The buttons show/hide, eg using:
(Alternatively, you can use just one button using the ternary operator for the condition.)
To display the number of items selected, I used a variable:
To retrieve the items from the database, you could add a button to populate a hidden form field, eg:
showItems.setValue(local1.data.idlist) and submit the form.
I’m not sure if this is the best way to do it and would be interested in comments/suggestions- eg:
Perhaps the local storage contents can be managed directly, without the array component.
Perhaps the IDs can be submitted without using a form.
I added a button to clear the local storage contents (to clear all selections), but don’t know how to clear the array without refreshing the page.