DOM not updating in x-for loop after sorting using SortableJS #4242
Unanswered
danbramall
asked this question in
1. Help
Replies: 1 comment 4 replies
-
Is there a reason why you can't use the Alpine Sort Core Plugin? Using index as the key will also cause tons of problems if you're sorting them, since alpine would be spending a lot of time overwriting and rewriting and not keep things synced well. You should add unique ids. Looked closer, there is also just an issue that you are implicitely updating the output, but if you use a getter, it will work fine. So the changes were basically
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have an AlpineJS UI which allows an editor to add style rules or groups of style rules to a page (it's not really important what these are but I've kept the concept so as to differentiate from a simpler application).
After adding rules and rule groups you can sort them by dragging/dropping. It uses SortableJS for this.
The rules created are output in JSON format so they can be copied/pasted into a third-party application.
HTML:
Typescript:
There's a CodePen here.
The issue I'm facing is with sorting. The code in the
onEnd
callback updates the rules object so that the JSON output shows the rules in the correct order, however the UI does not update — after dragging a rule to a new position the UI reverts back to how it was originally. If I comment out the stuff in theonEnd
callback the UI sorting works correctly but the JSON object doesn't change hence is now in the wrong order.I'd like the two things to be in sync so that the UI updates correctly when sorted and the JSON is also updated to reflect the new order.
Can anyone tell why this is happening and how it can be fixed?
Many thanks!
Beta Was this translation helpful? Give feedback.
All reactions