I am trying to create a menu using a repeat group. In that group I have the service name and price with a checkbox. When the checkbox is clicked I would like the price of the associated service to be added to the total. Any advice on how to accomplish that?
Hello One way to do this is to use a custom state which will hold a list of ācheckedā Services. Custom states are great because they allow us to āstoreā which Services a User has selected (until the page is refreshed), and custom states will not cost you any workflows since youāre not making any changes to the database. Here is an example in the forum app:
Preview:
Editor:
Here is a step-by-step:
To replicate this, you would set up a workflow which happens when the checkboxās value is changed. For example, you would select āElements ā When an Inputās Value is Changed ā Element: Checkboxā
In the above screenshots weāre just creating the When statement for when the Checkbox goes from āUncheckedā to āCheckedā. Then youād create an action in the workflow which adds the Current cellās service to the Selected List of Services. First, we need to set up the custom state List of Services. To do this, you would go to Element Actions ā Set State ā Element: checkbox_services, Custom state: Create a new custom stateā¦
Here weāre creating the custom state List of Services on the page itself (the pageās name is checkbox_services). Bubble allows us to create a custom state on any element, and the page is considered an element. We donāt want to set the custom state on the checkbox, because it wonāt be accessible to retrieve itās information using an outside text element, since the checkbox itself is inside of the repeating group.
After the custom state List of Selected Services on the page is created, the next step is to tell Bubble what the value of that List should be when a checkbox is checked. Since we need the Current cellās Service to be added to our list when this happens, the value of the custom state is: āChecked_ServiceāsSelectedServices:plus item Current cellās Serviceā.
This value tells Bubble to add (or ā:plusā) the Current cellās service to our Selected Services List when a User clicks a checkbox to go from unchecked to checked. Alternatively, we need to set up another workflow which will āminusā a Service from the Selected Services list if a checkbox goes from checked to unchecked (the User is trying to deselect a Service). To do this, weād set up another workflow where the āWhenā is: Elements ā An inputās value is changed ā Element: Checkbox and This Checkbox isnāt checked:
Everything is the same for setting the state on the Checkbox, and changing our Selected Services list. The only thing thatās different here will be the value (since weāre not doing :plus Current cellās Service; we need the service to be filtered out from our list):
This part is a little tricky because there isnāt a :minus option to remove a Service from the Selected Services list. As a workaround, we are going to use the :filtered option below:
This looks confusing at first, but this :filtered option is stating that the value of our Selected Services list cannot contain any Services whose unique ID matches the āCurrent cellās Serviceās unique IDā (the Service that the User just āuncheckedā).
Now that the workflows for checking and unchecking a service are set up, I created a repeating group and a text element on the right side of the page which display the Services a User has selected in the repeating group, and the total price of the Selected Services in the text element.
The data source for the repeating group is our Checkboxās Selected Services (shown below):
And the text element will display the :sum of the Selected Services āPriceā field. In this example, I set every Service to be $10.
And that should work as expected! If you have absolutely any questions or if any of this was confusing please feel free to let me know. It takes a little while to set up at first, but will definitely become easier after setting this up a few different times.
Thank you a lot. Iām going to give this a try!
Blown away by the level of detail in your answer.
Thank you!!
Hi,
Iām digging up this thread as I have tried to implement this solution on my app : https://bubble.io/page?type=page&name=index&id=scenarios&tab=tabs-1
The Checked workflow seems to work just fine, however the checked_options state does not seem to filter properly on āuncheckā, the display groups seem to be a bit funnyā¦ not sure where my error is, if someone could point me in the right direction it would be much appreciated !!
Thanks a lot !
Hey @anon18390936 Can you share some screenshots of the workflow you have when an element is unchecked? Or set the app to public by going to (Settings --> General --> Application Rights --> Everyone Can View)
Hi, I have actually made a workaround by creating a thing to store checked values and modifying it on check/uncheck, instead of a custom state, so that seems to do the job for nowā¦ I will explore states when iām not in such a rush !
Happy to share my (not so elegant and a bit cumbersome) solution if someone needs itā¦
thanks
Marj
Hi
your post is create but I still canāt find a solution in that case
please take a lookā¦
Thanks!
Hi @valeri.kopaleishvili I just took a look at the forum example again and it seems to be working, what is the question you have? Or did you mean to share a link to your app editor?
Problem in my case is when user choose multipule checkboxes value is saved as a one string in the database not as a different seperate inputs ( i guess so)
if i check only 1 checkbox its working and displays value correctly but in multiple case it doesnāt display any values
for detials you can have a look on my post
thank you!
What about if this checkbox value is stored in the database (when its checked)
and
what about if i want to delete this value from the database by unchecking this checkbox (value should be deleted)
I have a repeating group with one checkbox on the left, text in the middle, and another checkbox on the right (the user is clicking one of the checkboxes, one being āgoodā and the other being ābadā). I need to have a āSubmitā button at the end that allows the checkboxesā results update numbers in the database for that user. How can I make this work?
The :minus has been added since this post was created so no need to filter
Thank you so very much for such a detailed explanation.
Also, anyone who wants to actually write out the records from the state to a Thing (say on clicking a Save button) may be interested in this post:
Hi I have a question about this responseā¦ How come your plus: is current cellās service? Iām trying to use my checkbox to show only verified listings vs non verified. when I say plus: its not already focused on a particular cellā¦?