stimulus-sortable icon indicating copy to clipboard operation
stimulus-sortable copied to clipboard

How to deal with multiple lists with their own todos?

Open bradbajuz opened this issue 3 years ago • 0 comments

I've been trying to wrap my head around getting the sortable to work with multiple lists with todos and serializing. Similar to how jQuery Sortable works.

Todos in each list will always begin at 1 and increment as todos are added and decrement as todos are removed.

For example:

Todo List One has three todos

  • Todo 5, position: 1
  • Todo 1, position: 2
  • Todo 9, position: 3

Todo List Two has five todos

  • Todo 5, position: 1
  • Todo 1, position: 2
  • Todo 9, position: 3
  • Todo 17, position: 4
  • Todo 2, position: 5

In the current stimulus-sortable examples, this is how it works for only one list. Once any additional lists are added, the todos continue to be treated like they all belong to one list, so if there are 8 todos (three todos in List One and 5 todos in List Two), the respective positions will be 1-8.

A custom sort route would be needed as a collection that takes serialized data.

The sort action will do something like this:

  def sort
    params[:todo].each_with_index do |id, index|
      Todo.where(id: id).update_all(position: index + 1)
    end

    head(:ok)
  end

But I can't for the life of me figure out how to get this to work with stimulus-sortable. How do I get serialized data from the stimulus-sortable controller to my sort action?

bradbajuz avatar Apr 08 '21 13:04 bradbajuz