How to insert an element into a specific position in a list

Hello Bubble community,

I am currently working on an article writing app, which creates several sections (H2) and subsections (H3) for an article.

Users could want to insert a new H2 or H3 at a specific location. I know how to add something at the end of the list but not somewhere specific, can someone help me on this task?


Thanks a lot.

(I have another topic on this project about how to access the index of the big repeating group when another repeating group is stuffed inside)

You could add a number field to your H2/H3 things called ‘position’ (or even index, though that may confuse things).

You’d then need to have ‘insert’ buttons or icons in each line of every repeating group.

Every time a user clicks the insert icon on (for example) an H2, you’d initially run a workflow to add +1 to each H2 that has a higher ‘position’ than the clicked line, then another action to create the new H2 with a ‘position’ of the clicked line +1.

Simply sorting your lists by the new field afterwards would achieve what you’re looking for. This would of course also work in reverse, for the removal of sections.

1 Like

you cant control the position of the item in the list, the only thing you can do is present the data the way you want to by sorting it when its presented. so create an index field and sort by index in the repeating group.

when you add a new item, create a new index for that item that is “search for items, each ones index: max+1”

and what the person said above is good too, but if your trying to insert items between other items you need to generate the proper index that is a number between the 2 desired numbers.

that is a more advanced techniuqe but it helps to display what the index will be WHEN you create the thing. so have a text field that displays the new index your going to want so you can play with getting it right

Thank you for your help @ksplinter007 !

@ed19 so I tried your technique, and it worked perfectly well for my H2s. Thanks a lot !

Unfortunately for my H3s, I need to get the corresponding H2 index to be shure not to delete an H3 for another H2 section, and this is the topic of my another post.
I will call it solution since the main topic of the post found it, but i’d be glad to see you help me a little more on this !

Glad to help!

To take it a step further, when embedding a repeating group in bubble, any workflow actions generated in that line would only reference that line’s group. You’d be pretty safe with something like:
This repeating groups items:filtered (position > current cell’s position) position -1
delete thing: current cell’s H3

That said, what would probably be more stable (and give you a bit more page-wide control) would be creating a list of H3s field on your H2s, then likewise a field for the parent H2 on your H3s.

So when an H3 is created, all you’d need to do is reference the parent group’s thing (assuming you’ve got a parent group and have its type set to H2) in order to populate your parent field, applying the reverse simultaneously in adding it to the parent’s list of H3s.

Now when deleting, you’d be able to use search constraints as opposed to filtering, which is quite generally a lot faster and could be done from an element outside your repeating groups.

1 Like

Oh I see, it seems obvious now ! Thank you a lot

No problem!