Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Snapey's avatar

The alternative is to re-write the whole list every time (rebase it at zero with sequential numbers). Not a problem if there are only 4 entries, but this might be simplified example.

kendrick's avatar

@snapey - To start with the replacing, how would I get the order of the point I replace?

A re-write would also be interesting to see.

automica's avatar

@splendidkeen so is a point actually a bullet pointed item in a list? and your app has multiple lists and you can drag list items from one to another?

kendrick's avatar

@automica - Yes, two lists with points in it, switchable from left to right, vice versa.

automica's avatar

@splendidkeen do you need to interact with the list items on an individual basis or are you only ever displaying two lists? and is it 2 lists per user?

automica's avatar
automica
Best Answer
Level 54

@splendidkeen if there is no need to view list items on their own, I would ditch your points model and just add a json field to your List model which you can use to store a json object containing your list items.

That way, when you finish your interaction with your list, you just save the list items as an object into that field. You then don't need to worry about current orders or reordering as you can easily do that in js frontend.

Moving items from one list to another is done by a js method to add item to array, and at that point you remove it from the one it was previously in.

If you are making a todo app, then behavior may be even simplier.

For front end, I might use something like SortableJs shared lists http://sortablejs.github.io/Sortable/#shared-lists

automica's avatar

@splendidkeen no problem. I like a challenge :P

BTW I was watching a talk about coding the other evening on our local php group and one of the speakers said the following which resonated:

'if you can't work out how to write a test for the functionality you are writing then your code is too complicated'.

Whilst there is merit in your multiple sortable lists and for saving each item individually, in your usecase, its overkill.

Previous

Please or to participate in this conversation.