views:

1079

answers:

5

I've been implementing some nice interactive interfaces that can sort lists in my m rails app for models that use acts_as_list. I have a sort function that gets called and sets the position for each record afterr each drag and drop using the sortable_element script.aculo.us function.

This is an example of the controller action that handles the sort after the drag and drop completes:

  def sort
    params[:documents].each_with_index do |id, index|
      Document.update_all(['position=?', index+1], ['id=?', id])
    end
  end

Now I am trying to do this same thing with a model that is a nested set (acts_as_nested_set). An example of the type of interface interaction: http://script.aculo.us/playground/test/functional/sortable_tree_test.html

I am stuck on how to write the controller action to handle the sort when the drag and drop completes.

I've added the :tree=>true parameter to the sortable _element function so far which appears to send a list of hashes but it seems that I am still missing information about the entire nested order....

I was certain this has been done before and didn't want to try to reinvent the wheel, but I can't seem to find any examples of the controller action <-> view with js function setup to handle a sortable acts_as_nested_set

Any help with creating an interactive sortable nested set in rubyonrails would be appreciated!

Thanks,

John

+1  A: 

Are there built-in ways to match the order passed from params hash to the entire nested set of all of the items in the model?

Streamline
+1  A: 

Just found this:

sortable_element_for_nested_set on github

Looks like it'll do the job, however I'm having some bugs while trying to implement it. It basically makes the javascript return the id of the element that was moved, then goes through the elements and returns its new parent, left and right values. Can't believe it's taken this long for something like this to be written! Lucky it was just when I needed it :)

A: 

see example app here - http://github.com/matenia/jQuery-Awesome-Nested-Set-Drag-and-Drop

It's a hacky way of doing it, but its basically, sort first, then save order. Uses nestedsortables, serializelist, and 2 actions to traverse the tree

PS: I know this question is over a year old but hoping that the link above helps someone else coming here.

Matenia Rossides