Method: ActionView::Helpers::JavaScriptHelper#sortable_element

Defined in:
lib/action_view/helpers/javascript_helper.rb

#sortable_element(element_id, options = {}) ⇒ Object

Makes the element with the DOM ID specified by element_id sortable by drag-and-drop and make an AJAX call whenever the sort order has changed. By default, the action called gets the serialized sortable element as parameters.

This method requires the inclusion of the script.aculo.us JavaScript library.

Example:

<%= sortable_element("my_list", :url => { :action => "order" }) %>

In the example, the action gets a “my_list” array parameter containing the values of the ids of elements the sortable consists of, in the current order.

You can change the behaviour with various options, see script.aculo.us for more documentation.



399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/action_view/helpers/javascript_helper.rb', line 399

def sortable_element(element_id, options = {})
  options[:with]     ||= "Sortable.serialize('#{element_id}')"
  options[:onUpdate] ||= "function(){" + remote_function(options) + "}"
  options.delete_if { |key, value| AJAX_OPTIONS.include?(key) }
  
  [:tag, :overlap, :constraint, :handle].each do |option|
    options[option] = "'#{options[option]}'" if options[option]
  end
  
  options[:containment] = array_or_string_for_javascript(options[:containment]) if options[:containment]
  options[:only] = array_or_string_for_javascript(options[:only]) if options[:only]
  
  javascript_tag("Sortable.create('#{element_id}', #{options_for_javascript(options)})")
end