How to Assign a value to checkbox

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?

1 Like

Hello :slight_smile: 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. :slight_smile:

11 Likes

Thank you a lot. Iā€™m going to give this a try!

1 Like

Blown away by the level of detail in your answer.
Thank you!!

6 Likes

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 :slight_smile: 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 ! :wink:
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 :slight_smile: 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 :slight_smile:
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ā€¦?