FORUM COMMUNITY IDEAS SHOWCASE LEARNING CENTER

Delete a thing and then recalculate the number order in a list


#1

HI all, so the challenge:

Lets say I have a table and in that table I have two fields:

  • List order (number) - this is the number order in a list
  • Item name (text)

Looks like:

1, 'Item A'
2, 'Item B'
3, 'Item C'

So I have a work flow which allows the the deletion of a record, so lets say I delete the first Item A, I now have:

2 , 'Item B'
3 , 'Item C'

BUT: as part of the deletion process I want to update the Order (number) so that I now have:

1, 'Item B'
2, 'Item C'

How, as part of the deletion work-flow, how would I update the remaining record Order field to correctly change the order as above?

Many thanks!


#2

Yes, fighting this issue too. My users will be adding, deleting and re-arranging items in repeating groups and I would like a “sort order” field to be recalculated every time? Perhaps @mishav knows how - can this maybe be done with “list of numbers”? or in any other way.

I know this can be done with API workflow, but am interested in a more instant solution


#3

Having this issue as well. Anyone know how to do this please?


#4

I’m not sure how you will pull this off without using a schedule workflow on a list since you will have to run a workflow (update field values) on a list.
Even if you’re using Javascript to modify a list of numbers as you’re thinking at the end you will have to write this to your database.

I’ve done this before with the schedule a workflow on a list action. Basically you first need to store in a custom state the order number of the item to be deleted.
Then you delete the selected item.
You schedule an workflow on a list. The list will be a search for all items whose order number is greater than the deleted order number. (You only need to update those items below the deleted item, i.e. u want to shift those below up)
In the schedule workflow action you make change to the current item by decreasing its order number by 1.


#5

You could replace the order number with a linked list.

So a particular thing has links to “previous” and “next”.

Then a delete is just a case of updating two things pointers.

Now… displaying it in a repeating group is going to be interesting !


#6

The sort numbers do not have to be whole numbers. The easiest (might not be best) method for this would be to use the “Current cell’s index #” as a dynamic text element on the RG (must not be within a group, has to be ‘on’ RG base layer). Then the sort field:

First created thing in RG is given sort_order of 1, the next is given sort_order 2 and so on.

When a person deletes an entry, no big deal, the RG cell is removed and the sort order that shows to the end user is just the Current cell’s index # (always 1-x in single digit order). When a new thing is added to the middle of the RG between items 2 and 3 for instance, you would use the average of the two points and set the sort_order as 2.5

This scales infinitely since Bubble can store a calculated value over 10 decimal points. (2.25, 2.125, etc)


#7

@NigelG Ahhh good old linked list. Cool idea


#8

@philip This might work if you don’t care about the order numbers being non whole numbers.
The poster didn’t say if the order numbers are important or they’re just a means to sort the items.

In my specific application I needed the order numbers to be consecutive whole numbers.
This is because I was using this to get the next/previous element in my list from an external api call. So if I wanted to get the next element I will add 1 to the current element order and use that order number to retrieve the item. So having decimal point order numbers wouldn’t cut it for me.
If I was using non whole numbers I would have to do a search for all items greater than the current order. Sort them descending and pick the fist item. But I thought this was too cumbersome.

I think the approach one chooses will depend on their context. In my case the deletion operation will rarely occur but the search was going to be very frequent as my users will be making api calls to move from one item to the next one at a time. So it was better moving the complexity to the deletion instead of the searching.
Also my list is always going to be less than 50 so the workflow on list wasn’t going to be expensive.


#9

Nigel, would be interested in knowing more about a linked list option if possible?

Closest thing to a solution i have come up with so far is a workflow that simply swaps numbers using a +/- button. Works well as long as numbers are in sequence, but what to do when an item is deleted and numbers are no longer in sequence…

FYI all in this. I need the sequence to be in order at all times and instantly, but not nessecarily whole numbers and users do not need to see the numbers. So maybe I will try out @philip 's suggestion.


#10

This topic was automatically closed after 70 days. New replies are no longer allowed.