Add to list if not in list, remove from list if in list

I’m looking for a more efficient way to handle scenarios like repeating groups built as multi-select dropdowns.

I am sure there must be some obvious hack I’m missing, but I haven’t found it yet.

Scenario:

  • Store selected options in a repeating group or state variable
  • When user clicks a cell, need to toggle that option (add if not present, remove if present)

My current implementation (3 actions):

  1. ‘Is Selected’ custom event checks if option exists in list and returns data
  2. If not in list: Display list with :plus item
  3. If in list: Display list with :minus item

Is there a way to accomplish this toggle functionality in a single expression instead of three separate actions? If not, what do you think is the best way to do it?

Interesting question. I was always doing it as you explained as well. But I decided take this as a small challenge. I knew it was possible with the limited number of list operators Bubble has but I didn’t know how. I could finally find it after some half-hour effort :slight_smile: It doesn’t look bad but efficiency should be checked I guess.

Here is how it is running in a single expression:
chrome_ZalrmQDLKh

Here is the expression:

And here is how I came up with it with some test cases :slight_smile:

And here is the editor if you want to play with it: Tests for Forum 14 | Bubble Editor

1 Like

Blimey :rofl:

Very well done.

1 Like