tmbb
There are some online examples on how to reorder nested form inputs built with <.inputs_for/> using the Sortable.js library and streams. It’s all based on the new :sort_param provided by ecto.
I have a form with nested inputs in which I’d prefer not to use streams because there are several levels of nesting and I didn’t want to have to traverse the changeset to convert everything into streams and I don’t want to use drag and drop because the items to be reordered are quite “tall” and fill up too much of the screen to be dragged around easily.
From a UI point of view, I’d like to add buttons to move an element up the list (thus swapping it with the element above) and to move an element down (thus swapping it with the element below).
I’m having a real hard time with this, though. My naive solutions of updating the values of the :sort_param fields and emitting a change event are not working like I wanted, and I wonder if anyone else has already implemented this in a way that works.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
codeanpeace
Could you share what you tried and describe what isn’t working? Are you successfully updating/persisting the
:sort_paramaka position field and just having trouble with that change event?tmbb
I’ll paste what I think are the relevant parts:
The
<.add_new_item />component works perfectly, and it’s based on what the docs suggest.The
<.move_item/>, however is a bit more complex. It generates two buttons which when clicked swap the values of the following input value and changes a “dummy field” in order to re-submit the form, as in the code below.The code for that component is this:
The
:todos_sortis passed in the liveview parameters correctly, and the resulting changeset and form is generted correctly. It’s the DOM that doesn’t seem to be passed correctly.The parameters are passed into the following changeset:
This code is very integrated in the rest of the codebase, so it’s hard to show a self-contained example. I could generate anew app and build this form from scratch but that would require using tailwind components, which I’m not at all familiar with that.
siddd
Hi,
I would like some advice too.
I had to do button based reordering for multi-level(depth of 2) lists.
I have used two ways of doing it.
Have rewritten the code according to your use-case. Not tested
Method 1: Using sort_param
I have something like
<input name="todos[move_up]” />for each row.In “validate”, I modify the params like
Method 2: using custom event.
The button looks something like:
And the event handler looks something like
I am also saving the history as a simple list of structs so users can undo/redo changes.
Would love to hear if there are better ways to do that.
tmbb
Yes, these are good advantages. They come with the disadvantage of requiring a bit more code, though. A problem with your approach might happen if you extend your approach to two levels. Moving things up and down and creating nested children may be a problem if they are meant to be children of structs which don’t have an ID. If you don’t have an id, how can you identify the supposed parent fo the newly created or moved resource? This will mean you always have to create the parent before adding the child. This may or may not be a problem to you, depending on how you approach things.